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?
-
Did you use
req.query
? You're most likely usingreq.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 aboutname
. – nabroyan Commented Jun 22, 2015 at 11:04 - Then it's not the same routing ;) – Andrey Popov Commented Jun 22, 2015 at 11:15
2 Answers
Reset to default 3Use
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"
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745137593a4613276.html
评论列表(0条)