admin管理员组文章数量:1236710
so I'm in the process of refactoring my node/express application and I'm trying to separate my roots. Here's my issue:
I want an homepage and then a completely different page for extensions, that doesn't fall into other routes.
So for example, let's say I have 3 pages: Homepage, About, and then an user page where the URL is "mysite/username123".
Now I know (or at least believe) this can be accomplished by doing something like:
var express = require('express');
var router = express.Router();
router.get('/:slug', function(req, res, next) {
if(req.params.slug) {
var data = {
my: 'data'
};
res.render('index', data);
} else {
var userdata = {
my: 'data'
};
res.render('user', userdata);
}
});
module.exports = router;
But this isn't what I want to do; I would like to keep these in two different route files. So my app.js file would have something like:
var routes = require('./routes/index');
var users = require('./routes/users');
var about = require('./routes/about');
app.use('/', index);
app.use('/', users);
app.use('/about', about);
Where /users
renders if there is a slug. This actually sort of works. If there is no :slug
, the index file is loaded, otherwise the users file is loaded (which has the :slug in the route file, not shown here). But then /about
is overwritten.
Now I supposed I could shove /about
to the top of the list and that would work, but this seems extremely sloppy and the whole point of doing this is to properly structure my code.
How are situations like this supposed to be properly handled? Could someone please give me some help here?
so I'm in the process of refactoring my node/express application and I'm trying to separate my roots. Here's my issue:
I want an homepage and then a completely different page for extensions, that doesn't fall into other routes.
So for example, let's say I have 3 pages: Homepage, About, and then an user page where the URL is "mysite.com/username123".
Now I know (or at least believe) this can be accomplished by doing something like:
var express = require('express');
var router = express.Router();
router.get('/:slug', function(req, res, next) {
if(req.params.slug) {
var data = {
my: 'data'
};
res.render('index', data);
} else {
var userdata = {
my: 'data'
};
res.render('user', userdata);
}
});
module.exports = router;
But this isn't what I want to do; I would like to keep these in two different route files. So my app.js file would have something like:
var routes = require('./routes/index');
var users = require('./routes/users');
var about = require('./routes/about');
app.use('/', index);
app.use('/', users);
app.use('/about', about);
Where /users
renders if there is a slug. This actually sort of works. If there is no :slug
, the index file is loaded, otherwise the users file is loaded (which has the :slug in the route file, not shown here). But then /about
is overwritten.
Now I supposed I could shove /about
to the top of the list and that would work, but this seems extremely sloppy and the whole point of doing this is to properly structure my code.
How are situations like this supposed to be properly handled? Could someone please give me some help here?
Share Improve this question edited Jul 9, 2017 at 22:25 Ian Gray asked Jun 17, 2015 at 7:55 Ian GrayIan Gray 3871 gold badge4 silver badges15 bronze badges 6 | Show 1 more comment2 Answers
Reset to default 10It happens because /about
is overridden by /:slug
. In other words, express cannot tell if about
is not meant to be a slug
.
Solutions would be to either
Add a url param to let express differentiate the routes.
router.get('/users/:slug')
Skip to next route
router.get(':slug', function (req, res, next) { if (req.params.slug === 'about') { return next(); } // get user data and render res.render('user', userdata); });
Attach the /about
route below this.
Just rearrange your routes in this order
app.use('/about', about);
app.use('/:slug', users);
The static route will take priority over the dynamic route.
本文标签: javascriptExpress dynamic routing usingand slug as different route filesStack Overflow
版权声明:本文标题:javascript - Express dynamic routing usingand :slug as different route files - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739491379a2165607.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
/about
is overridden by/:slug
. In other words, express cannot tell ifabout
is not meant to be a slug – Swaraj Giri Commented Jun 17, 2015 at 8:02/:slug
in a separate file than the homepage at/
– Ian Gray Commented Jun 17, 2015 at 8:05router.get('/users/:id'
` – Swaraj Giri Commented Jun 17, 2015 at 8:12/:folder
and do the routing via a check inreq.params.folder
with aif/else
condition – Sergio Commented Jun 17, 2015 at 8:21