Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 为什么get总是走第一条路线_Node.js_Express - Fatal编程技术网

Node.js 为什么get总是走第一条路线

Node.js 为什么get总是走第一条路线,node.js,express,Node.js,Express,我在用快车 我有两条路线: router.get('/:id', function (req, res, next) { router.get('/search', function (req, res, next) { 每次我打电话给你http://localhost:3000/users/search?name=@我走的是第一条路线而不是第二条 我应该更改什么?因为/:id匹配任何包含/搜索的内容。它认为id是“搜索”。 尝试将“/search”路由放在第一位,或者将其更改为/find/

我在用快车

我有两条路线:

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

router.get('/search', function (req, res, next) {
每次我打电话给你http://localhost:3000/users/search?name=@我走的是第一条路线而不是第二条

我应该更改什么?

因为/:id匹配任何包含/搜索的内容。它认为id是“搜索”。 尝试将“/search”路由放在第一位,或者将其更改为/find/:id.

,因为/:id匹配任何包含/search的内容。它认为id是“搜索”。
尝试将“/search”路径放在第一位,或者将其更改为/find/:id。

您可以更改它们的顺序,这样应用程序只会在/search不匹配时检查/:id。或者,Express使用指向regexp的路径,该路径支持–例如/:id\d+仅匹配数字id。您可以更改它们的顺序,以便应用程序仅在/search不匹配时检查/:id。或者,Express使用指向regexp的路径,该路径支持–例如/:id\d+仅数字id匹配。