我想查询 Yelp API,并拥有以下路由:
app.get("/yelp/term/:term/location/:location", yelp.listPlaces)
当我向 http://localhost:3000/yelp?term=food&location=austin
发送GET请求时,我收到了以下错误:
Cannot GET /yelp?term=food&location=austin
我做错了什么?
我想查询 Yelp API,并拥有以下路由:
app.get("/yelp/term/:term/location/:location", yelp.listPlaces)
当我向 http://localhost:3000/yelp?term=food&location=austin
发送GET请求时,我收到了以下错误:
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
http://localhost:3000/yelp?term=food&location=austin
localhost:3000
/yelp
?term=food&location=austin
,即数据是 ? 之后的所有内容执行这些匹配时不考虑查询字符串,例如 "GET /" 将匹配以下路由,"GET /?name=tobi" 也将匹配。
因此,您应该:
req.query.term
/a
将处理/a?q=value
的请求。/a/:b
将匹配:
/a/b
/a/c
/a/anything
/a/b/something
或/a
。Express 4.18.1 更新:
使用 app.get("/yelp/term/:term/location/:location")
,你的查询字符串可以是 yelp/term/food/location/austin
因此,你的请求 URL 将会是这样的:
http://localhost:3000/yelp/term/food/location/austin