Comments (15)
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):
/api/projects.js
'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');
};
from express-enrouten.
That's a really nice idea. Currently I'm looking for a dynamic routing prefix. The underscored approach could be a good solution.
from express-enrouten.
from express-enrouten.
Yes, please. 👍
from express-enrouten.
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.
from express-enrouten.
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 express.Router()
a while back, and enrouten almost had everything. But the route params feature was something I wanted/needed too.
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
from express-enrouten.
@tlivings should probably chime in here.
from express-enrouten.
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 {param}
in the directory structure.
Example:
handlers
|--users
| |--{id}.js
| |--{id}
| |--foo.js
This will map as:
/users/{id}
/users/{id}/foo
I considered index.js
but decided against it since it didn't play well with subresources and index
is a valid path name in a url.
from express-enrouten.
PS: If there is no subresource, the above example would be simplified to:
handlers
|--users
| |--{id}.js
I'd personally like to remove index.js
from express-enrouten
as well.
from express-enrouten.
@tlivings: in your model /users
would be handled by ./handlers/users.js
rather than ./handlers/users/index.js
, correct? Going with that approach my question for the express-enrouten side would be how would you handle the root route?
from express-enrouten.
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 index.js
.
from express-enrouten.
/
is a perfectly acceptable path for a resource in a REST service too!
from express-enrouten.
There may not be anything stopping one from exposing resources at /
but I think it goes against convention of how resources are typically represented. We can argue offline though
from express-enrouten.
+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.
from express-enrouten.
late to the party here, but Nuxt has this style of routing
from express-enrouten.
Related Issues (20)
- Route middleware passed onto other routes
- Project Status
- Router level middleware HOT 11
- How to get all original paths of registered routes HOT 1
- Creating routes for subdomains HOT 2
- multer don't work on middlware HOT 1
- Is this over-designed? HOT 3
- Files with no extension are being processed by the directory traversal HOT 2
- Not able to upgrade engine-munger(version 1.1.1) HOT 1
- Non-JS files being accidently required
- directory: too quiet on requiring bad controller files
- Option to possibly use a different router than Express's default Router
- silently failing to require a route HOT 1
- Named routes HOT 1
- can config muilt directores
- Allow to ignore glob path HOT 3
- Jest incompatibility HOT 3
- Route name query HOT 1
- Support path-to-regexp somehow?
- Nested routes
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 express-enrouten.