Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global route for all controllers #40

Closed
jezikk opened this issue May 8, 2017 · 7 comments
Closed

Global route for all controllers #40

jezikk opened this issue May 8, 2017 · 7 comments

Comments

@jezikk
Copy link

jezikk commented May 8, 2017

Hi,

is there any suggested way how to set a "global" route for all controllers?

For example, I want to define this route:
/api/

for my controllers:

@Controller('user')
export class UserController

@Controller('customer')
export class CustomerController

with the following result:

/api/user
/api/customer

Thank you

@alexjoverm
Copy link

I was wondering about the same. In express you can prepend paths by using the Router, and then:

app.use('/a-route', aModule)

Is this existing in nest, or is a feature request?

@cojack
Copy link
Contributor

cojack commented May 11, 2017

@alexjoverm
Copy link

Very interesting, that library allows both global prefixes and per-controller prefixes, such as:

@Controller("/users")
export class UserController {
    // ...
}

kamilmysliwiec pushed a commit that referenced this issue May 23, 2017
…onfiguration class, multiple servers, global route prefix (#70, #20, #40)
@kamilmysliwiec
Copy link
Member

Hi @jezikk,
Since version 2.1.0 you can use setGlobalPrefix(prefix: string) method. Example:

const app = NestFactory.create(ApplicationModule);
app.setGlobalPrefix('api');

@flogvit
Copy link

flogvit commented Feb 25, 2019

Should this work with swagger?

import {NestFactory} from '@nestjs/core';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';

// ....
const app = await NestFactory.create(AppModule);
app.setGlobalPrefix('v1');
const options = new DocumentBuilder()
      .setTitle('title')
      .setDescription('API description')
      .setVersion('0.1')
      .addTag('tag')
      .build();
const document = SwaggerModule.createDocument(app, options);
SwaggerModule.setup('docs', app, document);

// ....

This doesn't add v1 as prefix in the documentation reflected on /docs/, but is reflected to the api, so I have to call /v1/... to get the endpoint.

Is this a bug?

@kgajowy
Copy link

kgajowy commented Apr 10, 2019

@flogvit

// document
    .setBasePath('v1')

As you can create multiple swagger documents (like, for every API version) it makes sense that it is no automatically derived from app

@lock
Copy link

lock bot commented Sep 23, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants