NestJS Logo

Global prefix

To set a prefix for every route registered in an HTTP application, call the setGlobalPrefix() method on the INestApplication instance.


const app = await NestFactory.create(AppModule);
app.setGlobalPrefix('v1');

To exclude routes from the global prefix, use the exclude option:


app.setGlobalPrefix('v1', {
  exclude: [{ path: 'health', method: RequestMethod.GET }],
});

Alternatively, you can specify a route as a string, which excludes it for every request method:


app.setGlobalPrefix('v1', { exclude: ['cats'] });
Hint The path property supports wildcard parameters through the path-to-regexp package. Bare wildcard asterisks (*) are deprecated; use parameters (:param) or named wildcards (*splat) instead. A named wildcard requires at least one extra segment, so health/*splat excludes /health/status but not /health itself. To exclude both, wrap the wildcard in braces: health/{*splat}.

Excluded routes and unmatched requests

Excluded routes are registered outside the prefix, but Nest still handles requests to them that don't match a handler. For example, with exclude: ['health'] and only a GET /health handler, a POST /health request goes through your exception filters and gets Nest's standard 404 response.

With the Express adapter, a request to a path that is neither under the prefix nor excluded never reaches Nest: Express answers it with its default 404 page. The Fastify adapter passes every unmatched request to Nest.

Hint With URI versioning enabled, exclusions are matched without the version segment, and an excluded route keeps it. For example, exclude: ['health'] with defaultVersion: '1' serves the route at /v1/health.
Edit on GitHub

Support us

Nest is an MIT-licensed open source project. It can grow thanks to the support of these awesome people. If you'd like to join them, please read more here.

Principal Sponsors

SerpApi LogoTrilon LogoMojam Logo

Sponsors / Partners

Become a sponsor