rumsky / nest-realworld

基于NestJS的Node后端全流程项目,包含api构建、MVC、用户验证(JWT)、连接MongoDB数据库、连接MySQL等。 Node Backend project based on NestJS, include build API、 MVC、User Authentication、Connect to MongoDB 、 Connect to MySQL

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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)

About

基于NestJS的Node后端全流程项目,包含api构建、MVC、用户验证(JWT)、连接MongoDB数据库、连接MySQL等。 Node Backend project based on NestJS, include build API、 MVC、User Authentication、Connect to MongoDB 、 Connect to MySQL


Languages

Language:TypeScript 98.7%Language:HTML 1.3%