NestJS Logo
On this page

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}.
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