Routing-controllers: Nesting controllers

Created on 8 Nov 2017  路  3Comments  路  Source: typestack/routing-controllers

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.

awaiting answer question

Most helpful comment

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.

All 3 comments

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.

Stale issue message

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

humbertowoody picture humbertowoody  路  4Comments

AleskiWeb picture AleskiWeb  路  4Comments

azaslonov picture azaslonov  路  5Comments

codedoge picture codedoge  路  6Comments

iangregsondev picture iangregsondev  路  3Comments