├── .gitignore ├── .prettierrc ├── .vscode └── settings.json ├── README.md ├── nest-cli.json ├── nodemon-debug.json ├── nodemon.json ├── package-lock.json ├── package.json ├── src ├── app.controller.ts ├── app.module.ts ├── app.service.ts ├── main.ts └── products │ ├── product.model.ts │ ├── products.controller.ts │ ├── products.module.ts │ └── products.service.ts ├── test ├── app.e2e-spec.ts └── jest-e2e.json ├── tsconfig.build.json ├── tsconfig.json └── tslint.json /.gitignore: -------------------------------------------------------------------------------- 1 | # compiled output 2 | /dist 3 | /node_modules 4 | 5 | # Logs 6 | logs 7 | *.log 8 | npm-debug.log* 9 | yarn-debug.log* 10 | yarn-error.log* 11 | lerna-debug.log* 12 | 13 | # OS 14 | .DS_Store 15 | 16 | # Tests 17 | /coverage 18 | /.nyc_output 19 | 20 | # IDEs and editors 21 | /.idea 22 | .project 23 | .classpath 24 | .c9/ 25 | *.launch 26 | .settings/ 27 | *.sublime-workspace 28 | 29 | # IDE - VSCode 30 | .vscode/* 31 | !.vscode/settings.json 32 | !.vscode/tasks.json 33 | !.vscode/launch.json 34 | !.vscode/extensions.json -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "trailingComma": "all" 4 | } -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "window.zoomLevel": 6 3 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | Nest Logo 3 |

4 | 5 | [travis-image]: https://api.travis-ci.org/nestjs/nest.svg?branch=master 6 | [travis-url]: https://travis-ci.org/nestjs/nest 7 | [linux-image]: https://img.shields.io/travis/nestjs/nest/master.svg?label=linux 8 | [linux-url]: https://travis-ci.org/nestjs/nest 9 | 10 |

A progressive Node.js framework for building efficient and scalable server-side applications, heavily inspired by Angular.

11 |

12 | NPM Version 13 | Package License 14 | NPM Downloads 15 | Travis 16 | Linux 17 | Coverage 18 | Gitter 19 | Backers on Open Collective 20 | Sponsors on Open Collective 21 | 22 | 23 |

24 | 26 | 27 | ## Description 28 | 29 | [Nest](https://github.com/nestjs/nest) framework TypeScript starter repository. 30 | 31 | ## Installation 32 | 33 | ```bash 34 | $ npm install 35 | ``` 36 | 37 | ## Running the app 38 | 39 | ```bash 40 | # development 41 | $ npm run start 42 | 43 | # watch mode 44 | $ npm run start:dev 45 | 46 | # production mode 47 | $ npm run start:prod 48 | ``` 49 | 50 | ## Test 51 | 52 | ```bash 53 | # unit tests 54 | $ npm run test 55 | 56 | # e2e tests 57 | $ npm run test:e2e 58 | 59 | # test coverage 60 | $ npm run test:cov 61 | ``` 62 | 63 | ## Support 64 | 65 | Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support). 66 | 67 | ## Stay in touch 68 | 69 | - Author - [Kamil Myśliwiec](https://kamilmysliwiec.com) 70 | - Website - [https://nestjs.com](https://nestjs.com/) 71 | - Twitter - [@nestframework](https://twitter.com/nestframework) 72 | 73 | ## License 74 | 75 | Nest is [MIT licensed](LICENSE). 76 | -------------------------------------------------------------------------------- /nest-cli.json: -------------------------------------------------------------------------------- 1 | { 2 | "language": "ts", 3 | "collection": "@nestjs/schematics", 4 | "sourceRoot": "src" 5 | } 6 | -------------------------------------------------------------------------------- /nodemon-debug.json: -------------------------------------------------------------------------------- 1 | { 2 | "watch": ["src"], 3 | "ext": "ts", 4 | "ignore": ["src/**/*.spec.ts"], 5 | "exec": "node --inspect-brk -r ts-node/register -r tsconfig-paths/register src/main.ts" 6 | } 7 | -------------------------------------------------------------------------------- /nodemon.json: -------------------------------------------------------------------------------- 1 | { 2 | "watch": ["dist"], 3 | "ext": "js", 4 | "exec": "node dist/main" 5 | } 6 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nestjs-intro", 3 | "version": "0.0.1", 4 | "description": "", 5 | "author": "", 6 | "license": "MIT", 7 | "scripts": { 8 | "build": "tsc -p tsconfig.build.json", 9 | "format": "prettier --write \"src/**/*.ts\"", 10 | "start": "ts-node -r tsconfig-paths/register src/main.ts", 11 | "start:dev": "concurrently --handle-input \"wait-on dist/main.js && nodemon\" \"tsc -w -p tsconfig.build.json\" ", 12 | "start:debug": "nodemon --config nodemon-debug.json", 13 | "prestart:prod": "rimraf dist && npm run build", 14 | "start:prod": "node dist/main.js", 15 | "lint": "tslint -p tsconfig.json -c tslint.json", 16 | "test": "jest", 17 | "test:watch": "jest --watch", 18 | "test:cov": "jest --coverage", 19 | "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", 20 | "test:e2e": "jest --config ./test/jest-e2e.json" 21 | }, 22 | "dependencies": { 23 | "@nestjs/common": "^6.0.0", 24 | "@nestjs/core": "^6.0.0", 25 | "@nestjs/platform-express": "^6.0.0", 26 | "reflect-metadata": "^0.1.12", 27 | "rimraf": "^2.6.2", 28 | "rxjs": "^6.3.3" 29 | }, 30 | "devDependencies": { 31 | "@nestjs/testing": "^6.0.0", 32 | "@types/express": "^4.16.0", 33 | "@types/jest": "^23.3.13", 34 | "@types/node": "^10.12.18", 35 | "@types/supertest": "^2.0.7", 36 | "concurrently": "^4.1.0", 37 | "jest": "^23.6.0", 38 | "nodemon": "^1.18.9", 39 | "prettier": "^1.15.3", 40 | "supertest": "^3.4.1", 41 | "ts-jest": "24.0.2", 42 | "ts-node": "8.1.0", 43 | "tsconfig-paths": "3.8.0", 44 | "tslint": "5.16.0", 45 | "typescript": "3.4.3", 46 | "wait-on": "^3.2.0" 47 | }, 48 | "jest": { 49 | "moduleFileExtensions": [ 50 | "js", 51 | "json", 52 | "ts" 53 | ], 54 | "rootDir": "src", 55 | "testRegex": ".spec.ts$", 56 | "transform": { 57 | "^.+\\.(t|j)s$": "ts-jest" 58 | }, 59 | "coverageDirectory": "../coverage", 60 | "testEnvironment": "node" 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/app.controller.ts: -------------------------------------------------------------------------------- 1 | import { Controller, Get, Header } from '@nestjs/common'; 2 | import { AppService } from './app.service'; 3 | 4 | @Controller() 5 | export class AppController { 6 | constructor(private readonly appService: AppService) {} 7 | 8 | @Get() 9 | @Header('Content-Type', 'text/html') 10 | getHello(): {name: string} { 11 | return {name: 'Max'}; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/app.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | 3 | import { AppController } from './app.controller'; 4 | import { AppService } from './app.service'; 5 | import { ProductsModule } from './products/products.module'; 6 | 7 | @Module({ 8 | imports: [ProductsModule], 9 | controllers: [AppController], 10 | providers: [AppService], 11 | }) 12 | export class AppModule {} 13 | -------------------------------------------------------------------------------- /src/app.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@nestjs/common'; 2 | 3 | @Injectable() 4 | export class AppService { 5 | getHello(): string { 6 | return 'Hello World!'; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { NestFactory } from '@nestjs/core'; 2 | import { AppModule } from './app.module'; 3 | 4 | async function bootstrap() { 5 | const app = await NestFactory.create(AppModule); 6 | await app.listen(3000); 7 | } 8 | bootstrap(); 9 | -------------------------------------------------------------------------------- /src/products/product.model.ts: -------------------------------------------------------------------------------- 1 | export class Product { 2 | constructor( 3 | public id: string, 4 | public title: string, 5 | public description: string, 6 | public price: number, 7 | ) {} 8 | } 9 | -------------------------------------------------------------------------------- /src/products/products.controller.ts: -------------------------------------------------------------------------------- 1 | import { 2 | Controller, 3 | Post, 4 | Body, 5 | Get, 6 | Param, 7 | Patch, 8 | Delete, 9 | } from '@nestjs/common'; 10 | 11 | import { ProductsService } from './products.service'; 12 | 13 | @Controller('products') 14 | export class ProductsController { 15 | constructor(private readonly productsService: ProductsService) {} 16 | 17 | @Post() 18 | addProduct( 19 | @Body('title') prodTitle: string, 20 | @Body('description') prodDesc: string, 21 | @Body('price') prodPrice: number, 22 | ) { 23 | const generatedId = this.productsService.insertProduct( 24 | prodTitle, 25 | prodDesc, 26 | prodPrice, 27 | ); 28 | return { id: generatedId }; 29 | } 30 | 31 | @Get() 32 | getAllProducts() { 33 | return this.productsService.getProducts(); 34 | } 35 | 36 | @Get(':id') 37 | getProduct(@Param('id') prodId: string) { 38 | return this.productsService.getSingleProduct(prodId); 39 | } 40 | 41 | @Patch(':id') 42 | updateProduct( 43 | @Param('id') prodId: string, 44 | @Body('title') prodTitle: string, 45 | @Body('description') prodDesc: string, 46 | @Body('price') prodPrice: number, 47 | ) { 48 | this.productsService.updateProduct(prodId, prodTitle, prodDesc, prodPrice); 49 | return null; 50 | } 51 | 52 | @Delete(':id') 53 | removeProduct(@Param('id') prodId: string) { 54 | this.productsService.deleteProduct(prodId); 55 | return null; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/products/products.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | 3 | import { ProductsController } from './products.controller'; 4 | import { ProductsService } from './products.service'; 5 | 6 | @Module({ 7 | controllers: [ProductsController], 8 | providers: [ProductsService], 9 | }) 10 | export class ProductsModule {} 11 | -------------------------------------------------------------------------------- /src/products/products.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable, NotFoundException } from '@nestjs/common'; 2 | 3 | import { Product } from './product.model'; 4 | 5 | @Injectable() 6 | export class ProductsService { 7 | private products: Product[] = []; 8 | 9 | insertProduct(title: string, desc: string, price: number) { 10 | const prodId = Math.random().toString(); 11 | const newProduct = new Product(prodId, title, desc, price); 12 | this.products.push(newProduct); 13 | return prodId; 14 | } 15 | 16 | getProducts() { 17 | return [...this.products]; 18 | } 19 | 20 | getSingleProduct(productId: string) { 21 | const product = this.findProduct(productId)[0]; 22 | return { ...product }; 23 | } 24 | 25 | updateProduct(productId: string, title: string, desc: string, price: number) { 26 | const [product, index] = this.findProduct(productId); 27 | const updatedProduct = { ...product }; 28 | if (title) { 29 | updatedProduct.title = title; 30 | } 31 | if (desc) { 32 | updatedProduct.description = desc; 33 | } 34 | if (price) { 35 | updatedProduct.price = price; 36 | } 37 | this.products[index] = updatedProduct; 38 | } 39 | 40 | deleteProduct(prodId: string) { 41 | const index = this.findProduct(prodId)[1]; 42 | this.products.splice(index, 1); 43 | } 44 | 45 | private findProduct(id: string): [Product, number] { 46 | const productIndex = this.products.findIndex(prod => prod.id === id); 47 | const product = this.products[productIndex]; 48 | if (!product) { 49 | throw new NotFoundException('Could not find product.'); 50 | } 51 | return [product, productIndex]; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /test/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import * as request from 'supertest'; 3 | import { AppModule } from './../src/app.module'; 4 | 5 | describe('AppController (e2e)', () => { 6 | let app; 7 | 8 | beforeEach(async () => { 9 | const moduleFixture: TestingModule = await Test.createTestingModule({ 10 | imports: [AppModule], 11 | }).compile(); 12 | 13 | app = moduleFixture.createNestApplication(); 14 | await app.init(); 15 | }); 16 | 17 | it('/ (GET)', () => { 18 | return request(app.getHttpServer()) 19 | .get('/') 20 | .expect(200) 21 | .expect('Hello World!'); 22 | }); 23 | }); 24 | -------------------------------------------------------------------------------- /test/jest-e2e.json: -------------------------------------------------------------------------------- 1 | { 2 | "moduleFileExtensions": ["js", "json", "ts"], 3 | "rootDir": ".", 4 | "testEnvironment": "node", 5 | "testRegex": ".e2e-spec.ts$", 6 | "transform": { 7 | "^.+\\.(t|j)s$": "ts-jest" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /tsconfig.build.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "exclude": ["node_modules", "test", "dist", "**/*spec.ts"] 4 | } 5 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "declaration": true, 5 | "removeComments": true, 6 | "emitDecoratorMetadata": true, 7 | "experimentalDecorators": true, 8 | "target": "es6", 9 | "sourceMap": true, 10 | "outDir": "./dist", 11 | "baseUrl": "./", 12 | "incremental": true 13 | }, 14 | "exclude": ["node_modules"] 15 | } 16 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "defaultSeverity": "error", 3 | "extends": ["tslint:recommended"], 4 | "jsRules": { 5 | "no-unused-expression": true 6 | }, 7 | "rules": { 8 | "quotemark": [true, "single"], 9 | "member-access": [false], 10 | "ordered-imports": [false], 11 | "max-line-length": [true, 150], 12 | "member-ordering": [false], 13 | "interface-name": [false], 14 | "arrow-parens": false, 15 | "object-literal-sort-keys": false 16 | }, 17 | "rulesDirectory": [] 18 | } 19 | --------------------------------------------------------------------------------