我要查询的叫喊API,并具有以下路线:的Node.js:快速app.get多个查询参数
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
我在做什么错?
我要查询的叫喊API,并具有以下路线:的Node.js:快速app.get多个查询参数
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
我想要添加到@ luto的答案。无需在路由中定义查询字符串参数。例如,路线/a
将处理/a?q=value
的请求。
的URL参数是一个快捷方式来定义所有的比赛路线的模式,使路线/a/:b
将匹配
/a/b
/a/c
/a/anything
它不会匹配
/a/b/something
或/a
在所请求的URL http://localhost:3000/yelp?term=food&location=austin
localhost:3000
/yelp
?term=food&location=austin
即数据是后一切?执行这些匹配时不会考虑查询字符串,例如“GET /”将匹配以下路由,如同“GET /?name = tobi”一样。
所以,你应该:
req.query.term