我正在构建一个新的应用程序,我的老板要求我生成这种 url 结构来搜索不同的项目:
-
xyz.com/<techs>
-
xyz.com/<locaions>
-
xyz.com/<sectors>
如您所见,这会导致在同一根级别上有 3 个不同的动态 url:
app.get('/', homeController.index);
...
app.get(':locations', locationsController.getLocations);
app.get(':techs', techsController.getTechs);
app.get(':sectors', sectorsController.getSectors);
...
显然尝试这种方法我最终只产生错误,并且在我看来这是不可能实现的。我错了吗?我的老板非常确定这是可能的...如果是,怎么做?
最佳答案
app.get('/:route', (req, res) => {
// to the name the name of the route parameter.
let route = req.params.route;
if (route === 'tech') {
// do sth
} else if (route === 'location') {
// do sth
} else {
// do sth.
}
})
关于javascript - 在根上表达动态路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46601341/