In a NestJS 12 app on the Express adapter, why does GET /users/me reach the @Get(':id') handler, and how do routeConflictPolicy and routeResolutionStrategy help?
answer
- registration follows declaration order
- Express adapter: first match wins
- pipes run after routing
- opt-in bootstrap diagnostics in 12
- literal before param before wildcard
basics
~10 sNest registers routes in declaration order and the Express adapter runs the first match, so an earlier @Get(':id') captures 'me'. NestJS 12's routeConflictPolicy reports this at bootstrap; routeResolutionStrategy: 'specificity' registers literal routes first.
solid answer
~40 sNest registers a controller's handlers on the HTTP adapter in the order they are declared. The Express adapter is order-sensitive: the first registered pattern that matches wins, and `:id` happily matches the literal `me`, so `findOne` runs with `id === 'me'`. Binding `ParseIntPipe` does not rescue it, because routing has already chosen the handler before any pipe runs; the request just becomes a 400. The classic fix is to declare static paths before parametric ones. NestJS 12 adds two opt-in `NestFactory.create()` options: `routeConflictPolicy` (`duplicate` and `shadow`, each `'off'`, `'warn'` or `'error'`) logs every overlapping pair or aggregates them into one `RouteConflictException` during `app.init()`, and `routeResolutionStrategy: 'specificity'` registers literal segments before parameters before wildcards. Both default to the old behaviour.
code
typescript · 14 linesimport { Controller, Get, Param } from '@nestjs/common';
@Controller('users')
export class UsersController {
@Get(':id')
findOne(@Param('id') id: string) {
return { handler: 'findOne', id };
}
@Get('me') // shadowed on Express: ':id' matches "me" first
findMe() {
return { handler: 'findMe' };
}
}go deeper
Recall the rule of thumb: in a NestJS controller on Express, declare literal paths such as 'me' before parametric ones such as ':id'.
Explain that the Express adapter dispatches to the first registered match and that pipes run only after a handler is chosen, so a ParseIntPipe turns the bug into a 400.
Show the NestJS 12 fix: shadow and duplicate levels in routeConflictPolicy, the aggregated RouteConflictException at init, and why 'specificity' can change which handler serves existing requests.
Weigh making routeConflictPolicy errors a house default against the churn it causes in older services, and whether 'specificity' or explicit ordering is the convention reviewers enforce.
## The symptom A users controller declares `@Get(':id') findOne()` first and `@Get('me') findMe()` second. The application boots without a word, `GET /users/42` works, and `GET /users/me` returns whatever `findOne` does with the id `'me'`: a "user not found", a database error on a non-numeric key, or a 400 if a pipe parses the id. `findMe` never runs. Nothing is wrong with either decorator; the problem is **registration order**. ## Why the Express adapter picks `:id` Nest reads the route metadata from each controller and hands every route to the HTTP adapter. What happens next depends on the adapter: - **Nest registers routes in declaration order.** Within one controller, the method declared first is registered first. - **The Express adapter is order-sensitive.** Express walks its routes in registration order and dispatches to the first pattern that matches the path. The adapter reports this through `isRouteOrderSensitive()`, which returns `true` for Express. - **A parameter segment matches any single segment.** `:id` captures `me` exactly as it captures `42`, so `/users/:id` is a match for `/users/me`. - **The Fastify adapter is different.** Its router ranks routes by specificity on its own, so declaration order does not decide the match there. The sequence for `GET /users/me` on Express is: 1. Express finds `/users/:id` first and binds `id = 'me'`. 2. Nest runs that route's guards, interceptors and pipes, then calls `findOne('me')`. 3. `/users/me` is never consulted, because a response has already been produced. ## What pipes can and cannot do It is tempting to bind `ParseIntPipe` to the id and expect `me` to "fall through" to the next route. It does not. **Routing selects the handler before any pipe runs**, so the pipe only sees a value for the handler that was already chosen. `ParseIntPipe` throws a `BadRequestException` ("Validation failed (numeric string is expected)"), and the client gets a 400 instead of the current user. Validation belongs to the pipes material; here it only proves that a pipe is not a routing tool. ## NestJS 12's two options Before version 12 the only defence was a convention: declare static paths before parametric ones, and review for it. NestJS 12 adds two **opt-in** properties to the options of `NestFactory.create()`: | Option | Values | Default | Effect | |---|---|---|---| | `routeConflictPolicy` | `{ duplicate?, shadow? }`, each `'off'`, `'warn'` or `'error'` | every kind `'off'` | Bootstrap-time diagnostics for overlapping routes | | `routeResolutionStrategy` | `'declaration'` or `'specificity'` | `'declaration'` | Order in which routes are registered on the adapter | How `routeConflictPolicy` behaves: - **`duplicate`** covers two routes with an identical method, path, host and version; **`shadow`** covers two patterns that can match the same request, such as `/users/me` and `/users/:id`. - **`'warn'`** logs one line per conflict, naming both handlers as `Controller#method`. - **`'error'`** collects every offending pair into a single `RouteConflictException`, thrown when the application initializes: in `app.init()`, or in `app.listen()` if `init()` was not called. You see all conflicts at once instead of one per restart. - The detector also compares **hosts** and, under header, media-type or custom versioning, **versions**: `@Version('1')` and `@Version('2')` on the same path do not conflict when their version sets do not intersect. `routeResolutionStrategy: 'specificity'` sorts routes before registering them: literal segments first, then parameters, then wildcards; routes that tie keep their declaration order. With the sort active, Nest drops the shadow conflicts the sort has already resolved, so `shadow: 'error'` does not abort an application whose routes now work. A genuine tie, such as `/users/:id` declared before `/users/:slug`, is still reported. ```typescript const app = await NestFactory.create(AppModule, { routeConflictPolicy: { duplicate: 'error', shadow: 'warn' }, routeResolutionStrategy: 'specificity', }); ``` ## On Fastify Apart from `duplicate`, both options only matter on order-sensitive adapters. On the Fastify adapter the `shadow` policy is a no-op and `'specificity'` sorting has no effect, while the `duplicate` policy is still honoured. ## Choosing a setting - Turning on `shadow: 'warn'` costs nothing and surfaces every existing shadow in the boot log. - `duplicate: 'error'` catches a copy-pasted route that would otherwise silently lose. - `'specificity'` changes which handler serves some requests in an existing application, so enable it with a test run, not on a Friday deploy. - Explicit ordering still reads best: even with the sort enabled, keeping literal routes above parametric ones makes the controller's intent obvious to the next reader.
- If routeResolutionStrategy is 'specificity', does shadow: 'error' still stop the app from booting?Only for shadows the sort did not fix. When the sort promotes a more specific route that was declared later, Nest drops that shadow before applying the policy, because the route now works. A tie the sort cannot break, such as `/users/:id` declared before `/users/:slug`, is still reported and still throws a `RouteConflictException`.
- Will routeConflictPolicy flag @Version('1') and @Version('2') handlers on the same path?No. Under URI versioning the version is part of the registered path, so `/v1/...` and `/v2/...` cannot overlap. Under header, media-type or custom versioning the detector compares version sets and reports only routes whose versions intersect, or where one side is unversioned or `VERSION_NEUTRAL`.
- Where does the RouteConflictException surface, and what does its message tell you?It is thrown while the application initializes: in `app.init()`, or in `app.listen()` when `init()` was not called first. The message starts with "Conflicting HTTP routes detected:", lists every pair with both handlers named, and ends by telling you to adjust the declarations or relax the `routeConflictPolicy` option.
A mail sorter that drops each letter into the first slot whose label fits: if the slot marked "any name" comes before the slot marked "me", your own letters land in "any name".
- Binding ParseIntPipe to the id makes GET /users/me fall through to findMe.
- Nest sorts routes by specificity by default on every adapter.
- NestJS 12 reports route conflicts out of the box without any option.
- The handler's method name decides which route wins.
- routeResolutionStrategy: 'specificity' changes matching on the Fastify adapter too.
Assumes NestJS 12; routeConflictPolicy and routeResolutionStrategy are new in 12 and both default to the earlier declaration-order behaviour.