2

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?

4
  • Did you use req.query? You're most likely using req.params. Commented Jun 22, 2015 at 11:00
  • I tested your program with req.query it worked perfectly fine. Commented Jun 22, 2015 at 11:01
  • I'm logging req.query and it shows { "id": "5" } and nothing about name. Commented Jun 22, 2015 at 11:04
  • Then it's not the same routing ;) Commented Jun 22, 2015 at 11:15

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

It makes no difference to use those question marks, they neither filter or reject a bogus header. The problem really is with curl. Try browser vs. curl.
This solution works well in browser. with no issues.
2

I found the problem. I was requesting via curl and it turns out that shell command 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"

2 Comments

Correct. Tried in browser, no issues. Also the specification says you are correct. expressjs.com/en/4x/api.html#req.query

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.