Global prefix for a module / route tree
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
I'm also looking for something like this , It would be awesome to have a Control of Module prefix or another way to achieve this
I agree, this will help keep our applications modular. At the moment I must specify my controller base as /api/users, /api/posts, /api/news etc. It's only a small inconvenience for small apps, but a redundancy nevertheless.
I think there should be a path property on parent modules, like id in Angular modules.
A reference/example on pure Javascript + Express of how to do it: http://katieleonard.ca/blog/2016/nested-routes-with-expressjs/
It's quite important with the issue #257 because in some cases you have nested elements:
\book:bookHash\chapter:chatperHash
And you want to test that bookHash exists (into the BookController) and also chapterHash exists (into the ChapterController)
The issue is that the modules are not always just a tree. Sometimes they look more like a graph, for example, there might be a module, UsersModule, which contains users business logic and the UsersController inside. The module prefix is users. Now few modules may depend on the UsersModule, but the UsersModule should not 'extend' they API prefixes. Irrelevant whether this is a good practice or not, but it's possible.
@kamilmysliwiec I don't know if it's a good or bad practice, but Angular has the ability of nested routes.
It's not obligatory and is easy to have a workaround, of course, but could be a good improvement.
@gelito nested routes are a good idea, but I can't see a good way to achieve this now. As I said above, module prefix may cause a lot of issues 🙁
Understood!
BTW great job for all!
isn't there any hack or some sort of middleware that would let us do that?
What is the possible issue with a module prefix?
@wbhob i have explained it below ^
Would it be possible to separate routing/controllers and things like services? The same way it's done in Angular for example, where you declare where to mount which modules into the routing tree but you can import and use a module in another without using any of it's routes?
@Kamshak hmm.. any ideas about the API? I'm trying to imagine how we could do it well
I think this will help @kamilmysliwiec https://github.com/angular/angular/blob/5.0.2/packages/router/src/router.ts#L190-L760 good luck :smiley: and also take a look add this article , it should give us any idea :+1: https://vsavkin.com/angular-2-router-d9e30599f9ea
@shekohex haha 😄 well, I know how it's working in Angular, I was asking about the Nest API proposal
Hahah 😂, sorry for misunderstand but here is what I'm trying to do right now I'm here trying to make a nested routes like a Parent route and a childs the simplest idea get in my mind is to make the ability to add a Parent controller to another one like this
@Controller('foo') class Foo { }
@Controller('bar', Foo) class Bar { }
then if I checked the Bar class route now it will be foo/bar instead of bar
what I did , is just played with reflect metadata
export function Controller(prefix?: string, parentController?: Controller): ClassDecorator {
let path = isUndefined(prefix) ? '/' : prefix;
if(parentController) {
const parentPath = Reflect.getMetadata(PATH_METADATA, parentController);
path = parentPath + '/' + path;
}
return (target: object) => {
Reflect.defineMetadata(PATH_METADATA, path, target);
Reflect.defineMetadata(PARENT_ROUTE, parentController, target);
};
}
However, I have it now working , but I think it is not the best solution for this issue so any ideas ?
@shekohex honestly, it's the best idea for now heh
I think it's just a Hack :smile:
anyway I'm now thinking how to make the child controller having access to his Parent eg: parms, body..
for example, If I have a 2 Controller PostsController, CommentsController
and what I want to achieve is to have this routes
GET posts/:id
GET posts/:id/comments
@Controller('posts')
export class PostsController {
@Get(':id')
getPosts(@Param('id') id: number) {
...
//go to database and returing the post
...
}
}
@Controller('comments', PostsController)
export class CommentsController {
@Get()
getPostComments() {
// now I need to get the controle of the post
// or post Id, to get it's comments
}
}
so any Help in this ? or any New Idea !
@shekohex
- Use middleware instead
- Attach post to req and call next()
@kamilmysliwiec: Will try to sketch out a quick consumer API. The challenge I see at the moment is that with the more declarative style you might loose some of the ease of the Decorators. (e.g. you'd always have to wire it up somewhere into the routing tree which might be tricky using decorators)