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

Nesting controllers #322

Closed
dcbartlett opened this issue Nov 8, 2017 · 3 comments
Closed

Nesting controllers #322

dcbartlett opened this issue Nov 8, 2017 · 3 comments
Labels
status: awaiting answer Awaiting answer from the author of issue or PR. type: question Questions about the usage of the library.

Comments

@dcbartlett
Copy link

I'm wondering because i don't see it in the documentation in the README, does the controller system all for nesting controllers. Express router supports this, just trying to find out if controllers do in routing-controllers.

Use case:

/api
/api/v1
/api/v1/user

i'd like to setup a controller for api that would point at a folder for its controllers. In that folder, i could setup multiple folders for v1, v2, etc. In those folders would be the controllers for the respective versions.

@NoNameProvided
Copy link
Member

You cannot do it the way you describe it, and you should not do it. however, you can do it. routing-controllers expects all of its routes to be preloaded at startup time like:

import 'reflect-metadata';
import { useExpressServer } from 'routing-controllers';
// other imports

import './routes';

class App { 
  public express: express.Application = express();
     
  constructor() {
    useExpressServer(this.express, {
      // your config
    });
  }
}

export default new App().express;

And you can prefix your routes via:

@JsonController('/v1/categories')
export class CategoriesRoute {
  // your route handlers here
}

So if you structure your routes into the routes/v1, routes/v2 folders and re-export them from an index.ts (a barrel) file, and then re-export it from routes then it will work.

// in routes/index.ts
export * from './v1';
export * from './v2';

However, I strongly advise you to not do this. Until you don't make a breaking change you don't have to update the version number and as soon as you need to you should copy-paste your current app, and create a separate app from it for v2.

If you don't do it sooner or later your app will become a mess as you need to keep all the old logic and a) monkey patch the new logic into it (I mean in services), b) start copying only services both way your codebase will start to be filled with hacks and duplicated code which will make maintenance superhard.

In the future, we may have some support for versioning though, a decorator like @Version('1.2') which will make it possible to manage the versioning in the same app.

@github-actions
Copy link

Stale issue message

@github-actions github-actions bot added the status: awaiting answer Awaiting answer from the author of issue or PR. label Feb 12, 2020
@github-actions
Copy link

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

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: awaiting answer Awaiting answer from the author of issue or PR. type: question Questions about the usage of the library.
Development

No branches or pull requests

3 participants