@AurelienLoyer && @EmmanuelDemey
@NestJS
@EmmanuelDemey
Consultant WEB
@AurelienLoyer
Software Engineer @QIMA
Nest (NestJS) is a framework for building efficient, scalable Node.js server-side applications
Node.js
TypeScript
Express or Fastify
npm install -g @nestjs/cli
nest new my-nest-project
// OR
npx @nestjs/cli new my-nest-project
.
|-- node_modules
|-- src
| |-- app.controller.ts
| |-- app.module.ts
| |-- app.service.ts
| `-- main.ts
|-- nest-cli.json
|-- package.json
|-- tsconfig.json
`-- .eslintrc.js
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
bootstrap();
Bootstrap our application
Main module definition
Port
And more...
import { Controller, Get } from '@nestjs/common';
@Controller('plants')
export class PlantsController {
@Get()
findAll(): string {
return 'This action returns all plants';
}
}
Use classes and decorators
Incoming requests
Returning responses
CRUD
import { Injectable } from '@nestjs/common';
import { Plant } from './interfaces/plant.interface';
@Injectable()
export class PlantsService {
private readonly plants: Plant[] = [];
create(plant: Plant) {
this.plants.push(plant);
}
//..
}
Use classes and decorators
Point of contact with the data
Injected as dependency
constructor(private catsService: CatsService) {}
import { Module } from '@nestjs/common';
import { PlantsController } from './plants.controller';
import { PlantsService } from './plants.service';
@Module({
controllers: [PlantsController],
providers: [PlantsService],
exports: [PlantsService],
})
export class PlantsModule {}
Use classes and decorators
Organize the application structure
Controllers / Providers / Exports
@Injectable()
export class LoggerMiddleware implements NestMiddleware {
use(req: Request, res: Response, next: NextFunction) {
console.log('Request...');
next();
}
}
Middleware
@Get(':id')
async findOne(@Param('id', ParseIntPipe) id: number) {
return this.catsService.findOne(id);
}
Pipes
Pipes
Guards
Interceptors
...
👨🏻💻 Live Coding Session!
Gardener!