2016-09-28 14 views
1

我正在使用快遞。爲什麼總是碰到第一條路線

我有2種途徑:

router.get('/:id', function (req, res, next) { 

router.get('/search', function (req, res, next) { 

我每次調用get http://localhost:3000/users/[email protected]我打的拳頭路由,而不是第二個嗎?

我應該改變什麼?

+1

您可以更改它們的順序,這樣當'/ search'不匹配時,應用程序將只會檢查'/:id'。或者,Express使用'path-to-regexp',它支持[提供帶參數的格式](https://www.npmjs.com/package/path-to-regexp#custom-match-parameters) - 例如'/:id(\ d +)'(僅數字'id'匹配)。 –

回答

4

因爲/:id匹配任何內容,包括/search。它認爲id是'搜索'。 嘗試先放置'/ search'路線,或將其更改爲/find/:id

相關問題