如何使Express.js与路径"/1.1.1"和"/ login"区分开来?
我使用以下代码:
app.get('/:x?.:y?.:z?', function(req, res){
...
app.get('/login', function(req, res){
Run Code Online (Sandbox Code Playgroud)
hro*_*oss 13
路由按添加顺序执行.因此,如果您希望登录路由优先,请先定义它.
否则,如果您想根据路由做出决策,可以从处理程序内部调用next()函数,如下所示:
app.get('/:x?.:y?.:z?', function(req, res, next){ // <== note the 'next' argument
if (!req.params.x && !req.params.y && !req.params.z) {
next(); // pass control to the next route handler
}
...
}
Run Code Online (Sandbox Code Playgroud)
从快速指南:"对于具有相同路径定义的几条路线也是如此,它们将按顺序执行,直到一个不调用next()并决定响应."
| 归档时间: |
|
| 查看次数: |
8290 次 |
| 最近记录: |