最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node JS - Express.js get query with multiple parameters - Stack Overflow

programmeradmin0浏览0评论

I'm quite new to JavaScript and Node JS and I have a such a situation. When I try to call get of express.js with a single parameter everything works fine, but when I try to call get with more than one parameter, it trims the query. For example I have such call and function

app.get('path/data', myFunc);
// in another file
function myFunc(req, res) {
    // do some stuff
}

When the url is path/data?id=5 or path/data?name=foo everything is fine. But when I use for example url like path/data?id=5&name=foo in myFunc I get url as path/data?id=5. So I get url's first part - what is before & sign.

Now what am I doing wrong? Is there something that I'm missing? How can I get whole url in myFunc without being trimmed?

I'm quite new to JavaScript and Node JS and I have a such a situation. When I try to call get of express.js with a single parameter everything works fine, but when I try to call get with more than one parameter, it trims the query. For example I have such call and function

app.get('path/data', myFunc);
// in another file
function myFunc(req, res) {
    // do some stuff
}

When the url is path/data?id=5 or path/data?name=foo everything is fine. But when I use for example url like path/data?id=5&name=foo in myFunc I get url as path/data?id=5. So I get url's first part - what is before & sign.

Now what am I doing wrong? Is there something that I'm missing? How can I get whole url in myFunc without being trimmed?

Share Improve this question edited Jun 22, 2015 at 11:11 nabroyan asked Jun 22, 2015 at 10:54 nabroyannabroyan 3,2756 gold badges40 silver badges59 bronze badges 4
  • Did you use req.query? You're most likely using req.params. – Bidhan Commented Jun 22, 2015 at 11:00
  • I tested your program with req.query it worked perfectly fine. – MJPinfield Commented Jun 22, 2015 at 11:01
  • I'm logging req.query and it shows { "id": "5" } and nothing about name. – nabroyan Commented Jun 22, 2015 at 11:04
  • Then it's not the same routing ;) – Andrey Popov Commented Jun 22, 2015 at 11:15
Add a ment  | 

2 Answers 2

Reset to default 3

Use

app.get('path/data?:id?:name')

And for retrieving the values, use req.query.id and req.query.name.

For accessing the REST api, you need to hit: http://localhost:8080/demo?id=3&name=stack

So, by this you can add multiple parameters in your api.

Hope this helps.

I found the problem. I was requesting via curl and it turns out that shell mand trims in case of there is an & in the url. So there is a need no add quotes like this

curl "path/data?id=5&name=foo"
发布评论

评论列表(0)

  1. 暂无评论