Node.js:使用多个查询参数来表示app.get
我想查询yelp API,并有以下路线:
app.get("/yelp/term/:term/location/:location", yelp.listPlaces)
当我向GET请求时
http://localhost:3000/yelp?term=food&location=austin
,
我得到错误
Cannot GET /yelp?term=food&location=austin
我究竟做错了什么?
你有没有试过像这样调用它?
http://localhost:30000/yelp/term/food/location/austin
您需要拨打的url通常看起来非常像路线,您也可以将其更改为:
/yelp/:location/:term
为了使它更漂亮:
http://localhost:30000/yelp/austin/food
在请求的urlhttp://localhost:3000/yelp?term=food&location=austin
- 基地址/地址是
localhost:3000
- 用于匹配的路由是
/yelp
- 查询stringurl编码的数据是
?term=food&location=austin
ie数据是一切后?
在执行这些匹配时不考虑查询string,例如“GET /”将匹配以下路由,“GET /?name = tobi”也一样。
所以你应该:
- 使用app.get(“/ yelp”)并从req.query中提取术语和位置,例如
req.query.term
- 使用app.get(“/ yelp / term /:term / location /:location”),但是相应的修改URL如上所述。
我想添加到@鲁托的答案。 无需在路由中定义查询string参数。 例如,路由/a
将处理/a?q=value
的请求。
url参数是定义路由模式的所有匹配的快捷方式,所以route /a/:b
将匹配
-
/a/b
-
/a/c
-
/a/anything
它不会匹配
/a/b/something
或/a