question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Global prefix for a module / route tree

See original GitHub issue

Hi,

there is an older issue, which got implemented: https://github.com/nestjs/nest/issues/40 - allowing to setGlobalPrefix('api') on the app instance.

This is a very neat feature and since I’m looking to create some sort of a “route tree”, it would be great to set some “route extending strategy”.

Say I want to create a route tree like this: -> api (from app.setGlobalPrefix) –> /users (UsersModule with prefix ‘users’ for all of it’s ‘children’ modules and controllers) —> /posts (PostsModule with prefix ‘posts’ for all of it’s ‘children’ modules and controllers) ----> /comments (Now I could declare a CommentsController with route ‘comments’ and this controller’s route would be composed from all of the ancestors above, in this case: ‘api/users/posts/comments’ ----> /upvotes (this one would then be ‘api/users/posts/upvotes’)

that means I’d like to somehow set the ‘module route prefix’ for all of it’s children’s modules & controllers.

If there’s already a way to achieve this, please point me in the right direction, otherwise it would be pretty nice to have this.

Thanks

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:10
  • Comments:41 (30 by maintainers)

github_iconTop GitHub Comments

25reactions
br0wncommented, Jan 26, 2018

@VinceOPS This is how we’ve done it so far

async function bootstrap() {
    const server = express();
    const config = require('../../etc/config.js');

    const apiFactory = new NestFactoryStatic();
    const api = await apiFactory.create(ApiModule, server);
    api.setGlobalPrefix("/api/v1");
    await api.init();

    const adminFactory = new NestFactoryStatic();
    const admin = await adminFactory.create(AdminModule, server);
    admin.setGlobalPrefix("/admin");
    await admin.init();

    http.createServer(server).listen(config.server.port);
}

bootstrap();
9reactions
wbhobcommented, Dec 9, 2017

I’ve opened a PR. This is sort of what we’re talking about, but in a more nest-friendly way. My intended usage is like this:

@Module({
    path: '/child-route'
})
export class MyChildModule { }

Essentially, at runtime, it reflects the parent module’s paths back onto each controller.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to add a route prefix to specific modules using NestJS?
I know I can use the undocumented function NestApplication.setGlobalPrefix to set a single global prefix: // main.ts app.setGlobalPrefix(version);.
Read more >
Global prefix - FAQ | NestJS - A progressive Node.js framework
Global prefix. To set a prefix for every route registered in an HTTP application, use the setGlobalPrefix() method of the INestApplication instance.
Read more >
Segment Routing Command Reference for Cisco NCS 5500 ...
Bandwidth-based local UCMP is performed for prefixes, segment routing Adjacency SIDs, and Segment Routing label cross-connects installed by IS-IS, and is ...
Read more >
prefix_route Module
The prefix_route module does routing based on a set of prefixes from the database. The prefix rule-set is loaded from the database into...
Read more >
A YANG Data Model for Routing Management RFC 8022
Otherwise, names are prefixed using the standard prefix associated with the corresponding YANG module, as shown in Table 1.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found