NestJS introduction

NestJS introduction

NestJS introduction made during Node.js Paris meetup (https://www.meetup.com/fr-FR/Nodejs-Paris/events/263164020/)

9298dcce5b2b0e88d8ffbb837192ca98?s=128

Sylvain Pontoreau

October 09, 2019
Tweet

Transcript

  1. 4.

    What is NestJS? • A Node.js MVC framework • Progressive

    • Written in TypeScript • Project organization inspired by Angular • A lot of modules: • Endpoint: Rest, GraphQL, WebSocket, ... • Data: TypeORM, Mongoose, ... • Communication: MQTT, RabbitMQ, gRPC, … • Others: Elastic Search, OpenAPI, Health checks, JWT, CQRS ... • Amazing documentation: https://docs.nestjs.com • Huge community (20k stars on Github, 120 contributors on the core) • CLI
  2. 5.

    Create a project • Install CLI: • Create a project:

    $ npm install –g @nestjs/cli $ nest new appName
  3. 6.

    Controllers import { Controller, Get } from '@nestjs/common'; @Controller() export

    class UserController { @Get('hello') get(): string { return 'Hello world!'; } } • Verbs: • @Get • @Post • @Put • @Delete • @Patch • ... • Actions & decorators: • @Param(key?: string) -> req.params • @Body(key?: string) -> req.body • @Headers(key?: string)-> req.headers • @Request(), @Response(), @Next() • ...
  4. 7.

    Services / Providers • A dependency to inject • Close

    to the Angular concept • Have to be injected • @Injectable • Example: import { Injectable } from '@nestjs/common'; @Injectable() export class UserService { findAll(): User[] { return [ // ... ]; } } import { Controller, Get } from '@nestjs/common'; import { UserService } from 'user.service'; @Controller() export class UserController { constructor( private readonly userService: UserService ) { } @Get() getAll(): User[] { this.userService.findAll(); } } user.service.ts user.controller.ts
  5. 8.

    Modules • Organize the application structure • A class annotated

    with @Module() • Contains: • providers, controllers, imports, exports • Example: import { Module } from '@nestjs/common'; import { UserService } from './user.service'; import { UserController } from './user.controller'; @Module({ imports: [], controllers: [UserController], providers: [ UserService, ], }) export class UserModule {}
  6. 9.

    Other capabilities • Middleware import { Injectable, NestMiddleware } from

    '@nestjs/common'; import { Request, Response } from 'express'; @Injectable() export class LoggerMiddleware implements NestMiddleware { use(req: Request, res: Response, next: Function) { console.log('Request...'); next(); } } import { Module, NestModule, MiddlewareConsumer } from '@nestjs/common'; import { LoggerMiddleware } from './common/middleware/logger.middleware'; import { UserModule } from './user/user.module'; import { UserController } from './user.controller'; @Module({ imports: [UserModule], }) export class AppModule implements NestModule { configure(consumer: MiddlewareConsumer) { consumer.apply(LoggerMiddleware) .forRoutes(UserController); } } logger.middleware.ts app.module.ts
  7. 10.

    Other capabilities • Validation with class-validator import { IsString, IsEmail,

    IsNotEmpty } from 'class-validator'; export class PostUserModel { @IsNotEmpty() @IsString() firstName: string; @IsNotEmpty() @IsString() lastName: string; @IsNotEmpty() @IsEmail() email: string; } import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import { ValidationPipe } from '@nestjs/common'; async function bootstrap() { const app = await NestFactory.create(AppModule); app.useGlobalPipes(new ValidationPipe()) await app.listen(3000); } bootstrap(); postUser.model.ts main.ts
  8. 11.

    Data access • Example with TypeORM: import { Entity, Column,

    PrimaryGeneratedColumn } from 'typeorm'; @Entity() export class User { @PrimaryGeneratedColumn() id: number; @Column() firstName: string; @Column() lastName: string; @Column() email: string; } import { createConnection } from 'typeorm'; export const databaseProviders = [ { provide: 'DatabaseConnection', useFactory: async () => await createConnection({ type: 'mysql', // ... }) }, ]; user.entity.ts database.provider.ts
  9. 12.

    Data access • Example with TypeORM: import { Injectable, Inject

    } from '@nestjs/common'; import { Repository, Connection } from 'typeorm'; import { User } from './user.entity'; @Injectable() export class UserService { private readonly userRepository: Repository<User> constructor( @Inject('DatabaseConnection') connection: Connection ) { this.userRepository = connection.getRepository(User); } async findAll(): Promise<User[]> { return await this.userRepository.find(); } } user.service.ts
  10. 13.

    Open API • Installation: import { NestFactory } from '@nestjs/core';

    import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'; import { ApplicationModule } from './app.module'; async function bootstrap() { const app = await NestFactory.create(ApplicationModule); const options = new DocumentBuilder() .setTitle('User API') .setDescription('The user API description') .setVersion('1.0') .addTag('user') .build(); const document = SwaggerModule.createDocument(app, options); SwaggerModule.setup('api', app, document); await app.listen(3000); } bootstrap(); $ npm install --save @nestjs/swagger swagger-ui-express • Decorators: • @ApiModelProperty • @ApiModelPropertyOptional • @ApiImplicitQuery • @ApiUseTags • @ApiResponse • @ApiBearerAuth • ...
  11. 14.
  12. 15.

    Resources • NestJS website: https://nestjs.com • NestJS & Fastify: https://docs.nestjs.com/techniques/performance

    • Demo (Github repository): https://github.com/spontoreau/nestjs-nodejs-paris