GithubHelp home page GithubHelp logo

Global route for all controllers about nest HOT 7 CLOSED

nestjs avatar nestjs commented on April 24, 2024 6
Global route for all controllers

from nest.

Comments (7)

kamilmysliwiec avatar kamilmysliwiec commented on April 24, 2024 35

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

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

from nest.

kgajowy avatar kgajowy commented on April 24, 2024 1

@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

from nest.

alexjoverm avatar alexjoverm commented on April 24, 2024

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?

from nest.

cojack avatar cojack commented on April 24, 2024

@jezikk or combine it with https://github.com/pleerock/routing-controllers

from nest.

alexjoverm avatar alexjoverm commented on April 24, 2024

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

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

from nest.

flogvit avatar flogvit commented on April 24, 2024

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?

from nest.

lock avatar lock commented on April 24, 2024

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.

from nest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.