# https://docs.nestjs.com/cli/overview
# Install Nest CLI
npm i -g @nestjs/cli
# Create new project
nest new nest-messages
cd nest-messages
# Run project
npm run start:dev
# Generate module
nest g module messages
# Generate controller (flat: no controller folder)
nest g controller messages/messages --flat
Decorator | Description |
---|---|
@Headers() | Access header information |
@Body() | Access the request body |
@Param('id') | Accesses request parameters |
@Query() | Access request query parameters |
At app bootstrap: Use a global validation pipe (middleware) to validate incoming request data.
app.useGlobalPipes(new ValidationPipe());
Description of how an object looks like that is transported from A to B.
export class CreateMessageDto {
@IsString()
content: string;
}
The emitDecoratorMetadata
allows DTO type information to survive compilation:
// TS: body type 'CreateMessageDto'
@Post()
createMessage(@Body() body: CreateMessageDto) {
console.log(body);
}
// JS: after compilation
__metadata('design:parmtypes', [create_message_dto_1.CreateMessageDto]);
Imagine to get a plain JSON object. Class transformer turns a plain object into a instance of a class.
Handles validation using decorators.
In the service file (and repository file) you have to add the @Injectable()
decorator to the class (or use the @Injectable()
decorator factory function
@Injectable()
export class MessagesService {}
In the Module file you have to add the service to the providers
array:
@Module({
controllers: [MessagesController],
providers: [MessagesService],
})