-
Notifications
You must be signed in to change notification settings - Fork 38
Feature Request: Dynamic parameter handling for directories #35
Comments
Interesting idea. I'll let others comment, too, but I need a little while to think on this. Just so you know you should also be able to do something like this (if it helps):
'use strict';
// api/projects.js
module.exports = function projects(router) {
router.get('/', function (req, res) {
res.send('ok');
};
router.get('/:taskId', function (req, res) {
res.send('ok');
};
router.get('/:taskId/team', function (req, res) {
res.send('ok');
};
router.get('/:taskId/team/:teamId', function (req, res) {
res.send('ok');
};
} Also, if you need it, this version of enrouten allows you to generate urls. Here's a (contrived) example: router.get({ name: 'projectById', path: '/:taskId' }, function (req, res) {
res.send('ok');
};
router.get({ name: 'projectTeams', path: '/:taskId/team' }, function (req, res) {
var task, team, data;
// get task and teams from somewhere
data = {
project: {
id: task.id,
uri: req.app.locals.enrouten.path('projectById', { taskId: task.id }),
teams: [{
id: team.id,
uri: req.app.locals.enrouten.path('projectTeamById', { taskId: task.id, teamId: team.id })
}]
}
};
res.json(data);
};
router.get({ name: 'projectTeamById', path: '/:taskId/team/:teamId'}, function (req, res) {
res.send('ok');
}; |
That's a really nice idea. Currently I'm looking for a dynamic routing prefix. The underscored approach could be a good solution. |
👍 Like the underscore idea for dynamic folders. |
Yes, please. 👍 |
For what it's worth, I've since changed my API design to have less deeply nested routes so this isn't really applicable to me any more. But, it looks like others might find it useful. |
I love the idea of enrouten. I have been using my "file-magik" module to auto-require/include all my routing files. But I was looking more after learning about Then I realized with just a few changes (which ended up being it's own module) I could use my existing module to do just what I was looking for. It doesn't have some of the bells and whistles of this project, but does have some this one doesn't... I wrote this quick and dirty module yesterday/today Check it out if interested: https://github.com/afloyd/express-routify |
@tlivings should probably chime in here. |
So, I've done something similar to this over at swaggerize-express. Since I am working off of swagger documents, path params are of the form Example:
This will map as:
I considered |
PS: If there is no subresource, the above example would be simplified to:
I'd personally like to remove |
@tlivings: in your model |
Yes, that's true. For RESTful services, there will always be a resource. For an application, there can be a root route, so it might not be possible to do away with |
|
There may not be anything stopping one from exposing resources at |
+1. I've been making some tests with this and it works ok, you have to remember to initialize the Router with { mergeParams: true } for this to work. |
late to the party here, but Nuxt has this style of routing |
I'm often in situations where I'd like to be able to separate out some deeply nested routes based on a
root/:param
type of situation.Here are some example routes to illustrate the points:
/api/projects
/api/projects/:taskId
/api/projects/:taskId/team
/api/projects/:taskId/team/:teamId
Right now, I'd have one big
index.js
file in the/api/projects
directory for all those routes. What do you think about a structure like this:Basically, directories with a leading _ would be converted to parameter routes. I mean, I'll admit it gets a little deep directory wise. But, you could always still just put everything right in the index.js. I think coupled with some CLI tooling/generators this wouldn't be that bad to whip out deep API handlers pretty quickly.
The text was updated successfully, but these errors were encountered: