Express的多个GET参数 [英] Multiple GET parameter with Express
问题描述
我是Node.js和Express的新手, 我一直在进行RESTful API项目,并且正在尝试通过URL发送带有多个参数的GET请求:
I'm new to Node.js and Express, I've been working on a RESTful API project, and I'm trying to send a GET request with multiple parameters in the URL:
这是我的路线:
Here is my route:
/centers/:longitude/:latitude
这是我尝试称呼它的方式:
and here is how I tried to call it:
/centers?logitude=23.08&latitude=12.12
我也尝试过
/centers/23.08/12.12
最终要走这条路线:
/centers/
那么我编写端点的方式是错误的吗?还是我要求的方式?
So is my way of writing the endpoint wrong? or the way I'm requesting it?
推荐答案
您没有正确理解Express中路由定义的工作原理.
You are not correctly understanding how route definitions work in Express.
这样的路由定义:
/centers/:longitude/:latitude
表示它期望这样的URL:
means that it is expecting a URL like this:
/centers/23.08/12.12
当您形成这样的URL时:
When you form a URL like this:
/centers?longitude=23.08&latitude=12.12
您正在使用查询参数(在?
之后的param=value
对).要访问这些内容,请参阅以下问题/答案:如何访问在?"之后获取参数在Express中?
You are using query parameters (param=value
pairs after the ?
). To access those, see this question/answers: How to access the GET parameters after "?" in Express?
为此,您可以为"/centers"
创建路由,然后访问req.query.longitude
和req.query.latitude
来访问那些特定的查询参数.
For that, you could create a route for "/centers"
and then you would access req.query.longitude
and req.query.latitude
to access those particular query parameters.
这篇关于Express的多个GET参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!