What flows down
A router gives what it has to everything under it: the routers made with its child(), and their endpoints. This page follows one folder, with a router, a child router and one endpoint:
src/endpoints/teams/
├── router.ts teams
└── $teamId/
├── router.ts $teamId, made with teams' child()
└── invite.endpoint.ts inviteexport default EndpointRouter.create({
imports: [DbModule],
context: { inject: { db: DbService } },
middleware: [RequestIdMiddleware],
guards: [SignedInGuard],
interceptors: [TimingInterceptor],
});import teamsRouter from '../router.js';
export default teamsRouter.child({
context: { params: z.object({ teamId: z.uuid() }) },
middleware: [LoadTeamMiddleware],
guards: [TeamMemberGuard],
interceptors: [AuditInterceptor],
});// src/endpoints/teams/$teamId/invite.endpoint.ts
import teamRouter from './router.js';
export default teamRouter.endpoint.mutation({
input: z.object({ email: z.email() }),
guards: [TeamAdminGuard],
handler: ({ params, input, db }) =>
db
.insert(invites)
.values({ teamId: params.teamId, email: input.email }),
});In the diagrams below: teams is the router, $teamId its child, and invite the endpoint.
Context and providers
The handler receives what every router above it declares in context:
| In the handler | Declared by |
|---|---|
db | teams context.inject |
params.teamId | $teamId context.params |
input | invite input |
$teamId also has the imports and providers of teams, so it lists only its own.
Lifecycle hooks
Each of the three can declare NestJS's lifecycle hooks in lifecycle. At each lifecycle event, NestJS calls that hook on all three, in this order:
Startup
onModuleInit()- teams router
- $teamId router
- invite endpoint
onApplicationBootstrap()- teams router
- $teamId router
- invite endpoint
Shutdown
onModuleDestroy()- invite endpoint
- $teamId router
- teams router
beforeApplicationShutdown()- invite endpoint
- $teamId router
- teams router
onApplicationShutdown()- invite endpoint
- $teamId router
- teams router
Shutdown runs on app.close(), or on a signal once the app calls enableShutdownHooks().
Guards and interceptors
A request for the endpoint passes each one once, from the outer router in:
POST /teams/42/invite
- Guards
- teams
SignedInGuard - $teamId
TeamMemberGuard - invite
TeamAdminGuard
- teams
- Interceptors, before the handler
- teams
TimingInterceptor - $teamId
AuditInterceptor
- teams
- Handler
- invite
handler()
- invite
The response
- Interceptors, after the handler
- $teamId
AuditInterceptor - teams
TimingInterceptor
- $teamId
An endpoint's own interceptors run after those of its routers too.
Middleware
Middleware runs first, before any guard, from the outer router in:
POST /teams/42/invite
- Middleware
- teams
RequestIdMiddleware - $teamId
LoadTeamMiddleware
- teams
- Then the guards, the interceptors and the handler
One instance each
Each provider, guard and interceptor of a router is one instance, for every endpoint and router under it: SignedInGuard is made once, with what teams provides and imports.
A child router that lists one again has an instance of its own. So does an endpoint, for a guard or an interceptor. Both instances run, the parent's first:
// src/endpoints/teams/$teamId/router.ts
export default teamsRouter.child({
// A second SignedInGuard, for this folder.
guards: [SignedInGuard, TeamMemberGuard],
});A nested router made with create()
It starts a context and providers of its own. The middleware, guards and interceptors of the routers above it still apply to its folder.
child() | create() | |
|---|---|---|
context | ✓ | |
imports and providers | ✓ | |
middleware, guards and interceptors | ✓ | ✓ |
| Lifecycle order | ✓ | ✓ |
A router listed in routers
A router another lists in its routers has everything a router made with that router's child() has, the context too, though it was made with create() in a package of its own.