| Decorator | Entity |
|---|---|
@Request(), @Req() |
req |
@Response(), @Res()* |
res |
@Next() |
next |
@Session() |
req.session |
@Param(key?: string) |
req.params / req.params[key] |
@Body(key?: string) |
req.body / req.body[key] |
@Query(key?: string) |
req.query / req.query[key] |
@Headers(name?: string) |
req.headers / req.headers[name] |
@Ip() |
req.ip |
@HostParam() |
req.hosts |
- controller
nest g controller users - module
nest g module users - service
nest g service users
- deal with incoming request and return response.
- generator
nest g controller users
- module is the place where you register your controller
- module is used to organize the codebase.
- module is always
singleton. nest g controller userswill automatically register controller inapp.module.ts
- it can inject dependencies.(DI is a pattern following the
Open-closed princile) - include
services,repositories,factories,helpersand so on. - decorators:
@Injectable
- create a service
nest g service users
- limit lifetime of
providerswithin theapplication'slifetime - for example. scope to a
requestlifttime.
- called
beforethe route handler - the same in
express - config
routeshere
- nest has built-in gloabl error handler
- you can throw error with http code
@Get()
async findAll() {
throw new HttpException({
status: HttpStatus.FORBIDDEN,
error: 'This is a custom message',
}, HttpStatus.FORBIDDEN);
}- What about naming conventions in Nest.js?