Comments (7)
it the obvious result as any Controller
or Module
should have only a single route. with that have said,
expressjs supports routes alias as an array
so we can use
app.get(['/user/:id', '/profile/:id'], (req, res) => { ... });
as nest-router uses the build-in MODULE_PATH
which is provided in nestjs/core
we cannot provide it as an array.
so we have a workaround here:
you can create another module with the same name and post-fix it with v2 or whatever,
and also do that for your controller, actually you would not have to re implement the same methods of that controller you can extend it and override the changed API/method of the old method or create new one, nest should get the old methods too.
to be clear i'll make it as an example:
we have module Users
and in that module we have one controller UserPayment
...
@Controller('/payment')
export class UserPayment {
@Get(':id')
async getUserPayments(@Parm('id') userId: number) {
...
// some complex sql calls
}
...
// another methods
}
...
@Module({
controllers: [UserPayment]
})
export class Users {}
then we have updated our api and now we have api v2
we will create another module UsersV2
and another UserPaymentV2
controller
...
@Controller('/payment')
export class UserPaymentV2 extends UserPayment {
@Get(':id')
async getUserPayments(@Parm('id') userId: number) {
...
// some complex sql calls
// with update for v2
}
...
// another methods from extended class
}
...
@Module({
controllers: [UserPaymentV2]
})
export class UsersV2 {}
then in our routes
const routes: Routes = [
{
path: 'v1',
children: [{ path: 'users', module: Users }],
},
{
path: 'v2',
children: [{ path: 'users', module: UsersV2 }],
},
];
PS:
children
array can be just an array of modules if they have the same path.
from nest-router.
@shekohex Do you think this is a good idea? Let say we will have 3 versions. In the user folder, there will be 3 files for each user.module.ts and user.controller.ts
Which seems very hard to maintain. Any idea?
from nest-router.
@vahidvdn so you have 3 versions of the same Controller?
The easiest way is that you every time you create a new version you extend the original Controller class and override/add any new methods you want.
And you version them in the routes file.
from nest-router.
@shekohex Thanks for the point.
What I mean was about managing files. Let say we will have module, moduleV2, and moduleV3. Also for controllers. It seems very messy.
from nest-router.
Did you tried to use the same Module, but with 3 Different Controllers?
ModuleA: ControllerV1, ControllerV2, ControllerV3
Every one of these extends the previous one with new changes and with a new route in the @Controller()
Is that ok?
from nest-router.
@shekohex No, that's not. In your example:
const routes: Routes = [
{
path: 'v1',
children: [{ path: 'users', module: Users }],
},
{
path: 'v2',
children: [{ path: 'users', module: UsersV2 }],
},
];
In v2 path, you mentioned UsersV2 module. So, as I understand we can't have only one module. Am I right?
from nest-router.
Yes, any module will only have one version, but as many Controllers you wish, so I guess you could instead of using this package you could add the versions to the Controllers itself.
If you don't need to versioning based on the modules.
from nest-router.
Related Issues (20)
- There is no such a word like childrens in english HOT 1
- Params in nested routes and swagger HOT 3
- Difference to nestjs RouterModule ? HOT 1
- How to use versioning in v7? HOT 1
- dynamic path intercepts static HOT 4
- Require controller and http method instead of module HOT 1
- Intercepter per route (module) HOT 3
- An in-range update of @nestjs/core is breaking the build 🚨 HOT 3
- An in-range update of @nestjs/core is breaking the build 🚨 HOT 1
- Route resolving fails when setGlobalPrefix() is used HOT 5
- Nest-Router Adding Additional Slash before Controller HOT 4
- An in-range update of @nestjs/core is breaking the build 🚨 HOT 1
- Route gets double slash when try to define root level routes; `//users` instead of `/users` HOT 6
- Add a name to the routes HOT 3
- Defining routes as both nested and top level HOT 3
- Lack of documentation HOT 1
- Nested modules does not retain prefixes. HOT 2
- Child routes with parent modules HOT 2
- Question : What are the drawbacks of having route prefix at controller level instead of module level ? HOT 2
- [Problem] Order of evaluation of routes is the other way round HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nest-router.