https://stackoverflow.com/questions/47338471/express-routing-multiple-urls-to-the-same-route?rq=1
Instead of using an anonymous function as the route's controller, you can give it a name and pass the name to router.get
. You can then have several router.get
s that points to the same function.
function slugController(req, res, next) {
if (!req.params.slug) {
req.params.slug = 'home'
}
getData(slug, function(err, data){
res.render('index', data)
});
});
router.get("/page-slug-name", slugController);
router.get("/page-slug-name/amp", slugController);
router.get("/", slugController);
router.get("/amp", slugController);
This works best if there only are a couple of routes.
If you have a ton of routes you have to use the regex stuff that's mentioned in the manual. I don't see any pattern in your URLs though, so it's a bit hard to come up with a good solution using regex.
Không có nhận xét nào:
Đăng nhận xét