Skip to content

Latest commit

 

History

History
160 lines (112 loc) · 4.31 KB

README.md

File metadata and controls

160 lines (112 loc) · 4.31 KB

Nest Logo

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

NPM Version Package License NPM Downloads Travis Linux Coverage Gitter Backers on Open Collective Sponsors on Open Collective

Description

Nest framework TypeScript starter repository.

Installation

$ npm install

Running the app

# development
$ npm run start

# watch mode
$ npm run start:dev

# production mode
$ npm run start:prod

Test

# unit tests
$ npm run test

# e2e tests
$ npm run test:e2e

# test coverage
$ npm run test:cov

TECHNIQUES

Midddleware

New middleware

nest g mi Logger

Applying middleware

  • AppModule implements NestModule
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer .apply(LoggerMiddleware) .forRoutes('cats';
  }
}
  • Use Globale Middleware main.ts: app.use(logger)

Pipe

内置 Pipe

  • ValidationPipe 必须先安装 class-validator and class-transformer
  • Use Global Pipe

Dto (Data Transfer Object)

Request payloads 使用 class 定义

Entity(Model)

使用 interface 定义

Controller

@Get @Post @Redirect @HttpCode @Header @Param @Body return Promise

Database

TypeORM Intergration

ORM(Object Relational Mapper)

  • install dependencies

npm install --save @nestjs/typeorm typeorm mysql

  • import the TypeOrmModule into the root AppModule
import { TypeOrmModule } from '@nestjs/typeorm';

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'mysql',
      host: 'localhost',
      port: 3306,
      username: 'root',
      password: 'root',
      database: 'test',
      entities: [],
      synchronize: true,
    }),
  ],
})

或者将数据库配置放到 ormconfig.json 中

Authentication

$ npm install --save @nestjs/passport passport passport-local

$ npm install --save-dev @types/passport-local

Security

  • Helmet
  • CORS
  • CSFR
  • Rate Limiting

Roadmap

  • Env Configuration
  • Security
  • GraphQL
  • OpenAPI (Swagger)