---
url: /guide/what-flows-down.md
description: >-
  What flows down — what a router gives the routers and endpoints under it, on
  one example — context and providers, lifecycle hooks, guards, interceptors and
  middleware, and the order each runs in.
---

# 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  invite
```

::: code-group

```ts [teams/router.ts]
export default EndpointRouter.create({
  imports: [DbModule],
  context: { inject: { db: DbService } },
  middleware: [RequestIdMiddleware],
  guards: [SignedInGuard],
  interceptors: [TimingInterceptor],
});
```

```ts [teams/$teamId/router.ts]
import teamsRouter from '../router.js';

export default teamsRouter.child({
  context: { params: z.object({ teamId: z.uuid() }) },
  middleware: [LoadTeamMiddleware],
  guards: [TeamMemberGuard],
  interceptors: [AuditInterceptor],
});
```

:::

```ts
// 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 {#context}

The handler receives what every router above it declares in [`context`](./router-context.md):

| 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 {#lifecycle}

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()`
  1. teams router
  2. $teamId router
  3. invite endpoint
* `onApplicationBootstrap()`
  1. teams router
  2. $teamId router
  3. invite endpoint

**Shutdown**

* `onModuleDestroy()`
  1. invite endpoint
  2. $teamId router
  3. teams router
* `beforeApplicationShutdown()`
  1. invite endpoint
  2. $teamId router
  3. teams router
* `onApplicationShutdown()`
  1. invite endpoint
  2. $teamId router
  3. teams router

Shutdown runs on `app.close()`, or on a signal once the app calls `enableShutdownHooks()`.

## Guards and interceptors {#guards}

A request for the endpoint passes each one once, from the outer router in:

**`POST /teams/42/invite`**

* Guards
  1. teams `SignedInGuard`
  2. $teamId `TeamMemberGuard`
  3. invite `TeamAdminGuard`
* Interceptors, before the handler
  1. teams `TimingInterceptor`
  2. $teamId `AuditInterceptor`
* Handler
  1. invite `handler()`

**The response**

* Interceptors, after the handler
  1. $teamId `AuditInterceptor`
  2. teams `TimingInterceptor`

An endpoint's own [`interceptors`](../reference/endpoint.md#options) run after those of its
routers too.

## Middleware {#middleware}

Middleware runs first, before any guard, from the outer router in:

**`POST /teams/42/invite`**

* Middleware
  1. teams `RequestIdMiddleware`
  2. $teamId `LoadTeamMiddleware`
* Then the guards, the interceptors and the handler

## One instance each {#instances}

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:

```ts
// 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()` {#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` {#routers}

A router another lists in its [`routers`](./routers.md#a-router-from-another-package) 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.
