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.

[Feature, Question] Is it possible to create a controllers' hierarchy? (Similar to using Route in express)

See original GitHub issue

Here is a use-case:

  1. I have
@Controller('users')
class UsersController ...
  1. Then I have a separate router that handles specific users information, like
@Controller('users/products')
class ProductsController ...

Same applies to URL REST versioning when you need to define couple controllers under the same path prefix:

  • /v1/users
  • /v1/orders
  • /v1/products

This particular case can be resolved with app.setGlobalPrefix('api/v1');

But then we need to support couple API versions, and some of the routes should be reused from the previous version

  • /v1/users
  • /v2/orders
  • /v2/products

How can that be resolved?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
xeoneuxcommented, Sep 13, 2017

@kamilmysliwiec this looks like a major use case.

5reactions
br0wncommented, Nov 10, 2017

modules are completely independent of http server

@kamilmysliwiec I’m not sure exactly what you meant by this, could you please elaborate? Since controllers are specified in @Module decorator and they provide routing I was under assumption that modules are in some extent aware of controllers.

My feature request would be to group controllers under common route prefix. Module seems to be prefect place for that as it groups a number of controllers together.

At the moment I’m doing this by having multiple instances of nest app:

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();
Read more comments on GitHub >

github_iconTop Results From Across the Web

Express Tutorial Part 4: Routes and controllers - MDN Web Docs
In this tutorial we'll set up routes (URL handling code) with "dummy" handler functions for all the resource endpoints that we'll eventually ...
Read more >
Routing with NodeJS (Express) - Medium
Here we say that our routes folder will be in /api/routes/ and we say express to use it. The hierarchy of the routes...
Read more >
Rest with Express.js nested router - Stack Overflow
You can nest routers by attaching them as middleware on an other router, with or without params . You must pass {mergeParams: true}...
Read more >
Organizing your Express.js project structure for better ...
The next folder is controllers , which will house all the controllers needed for the application. These controller methods get the request from ......
Read more >
Project structure for an Express REST API when there is no ...
As you can see from the code above, no logic should go in your routes/routers . They should only chain together your controller...
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