nabroyan
nabroyan

Reputation: 3275

Node JS - Express.js get query with multiple parameters

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?

Upvotes: 2

Views: 5902

Answers (2)

Aarzoo Trehan
Aarzoo Trehan

Reputation: 127

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.

Upvotes: 3

nabroyan
nabroyan

Reputation: 3275

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"

Upvotes: 2

Related Questions