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 users
will automatically register controller inapp.module.ts
- it can inject dependencies.(DI is a pattern following the
Open-closed princile
) - include
services
,repositories
,factories
,helpers
and so on. - decorators:
@Injectable
- create a service
nest g service users
- limit lifetime of
providers
within theapplication's
lifetime - for example. scope to a
request
lifttime.
- called
before
the route handler - the same in
express
- config
routes
here
- 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?