2016-11-08 55 views
0

符合express3的書。在以下情況下,即使下一個路線也與該模式匹配,http://localhost:3000/abcd將始終打印「abc *」。路線優先順序表3和4

我的問題是以同樣的方式工作express4?

app.get('/abcd', function(req, res) { 
    res.send('abcd'); 
}); 
app.get('/abc*', function(req, res) { 
    res.send('abc*'); 
}); 

顛倒順序將其打印「ABC *」:

app.get('/abc*', function(req, res) { 
    res.send('abc*'); 
}); 
app.get('/abcd', function(req, res) { 
    res.send('abcd'); 
}); 
+0

在這兩種表達3級4的版本與被請求匹配的第一個路由處理路線獲勝。 – undefined

+0

謝謝,那是舊書或錯誤。謝謝 ! – stackdave

回答

0

該路由匹配的第一個路由處理程序是被調用的一個。這就是Express在所有最新版本中的工作原理。你通常應該從更具體到不那麼具體的路線指定你的路線,然後更具體的路線將首先匹配,不太具體的路線將會抓住其餘的路線。

如果你想有一個處理程序來查看所有比賽,然後傳給其他處理的事情,你一般會使用中間件:

// middleware 
app.use("/abc*", function(req, res, next) { 
    // process request here and either send response or call next() 
    // to continue processing with other handlers that also match 
    next(); 
});