是否可以像$_GET
在PHP中一样在Node.js中获取查询字符串中的变量?
我知道在Node.js中,我们可以在请求中获取URL。有没有获取查询字符串参数的方法?
是否可以像$_GET
在PHP中一样在Node.js中获取查询字符串中的变量?
我知道在Node.js中,我们可以在请求中获取URL。有没有获取查询字符串参数的方法?
You can use
request.query.<varible-name>;
It is so simple:
Example URL:
http://stackoverflow.com:3000/activate_accountid=3&activatekey=$2a$08$jvGevXUOvYxKsiBt.PpMs.zgzD4C/wwTsvjzfUrqLrgS3zXJVfVRK
You can print all the values of query string by using:
console.log("All query strings: " + JSON.stringify(req.query));
Output
All query strings : { "id":"3","activatekey":"$2a$08$jvGevXUOvYxKsiBt.PpMs.zgzD4C/wwTsvjz fUrqLrgS3zXJVfVRK"}
To print specific:
console.log("activatekey: " + req.query.activatekey);
Output
activatekey: $2a$08$jvGevXUOvYxKsiBt.PpMs.zgzD4C/wwTsvjzfUrqLrgS3zXJVfVRK
From my point of view I think that many people mix two different concepts. During the REST development I was familiar with passing information in the URL with two ways "path variables" and "request parameters"(query parameters). The RFC describes the parts of URI like this: enter link description here So I understood that author would like to know how to pass request parameters. I would only want to make the topic easier to understand, but the solution was mentioned here many times.
You can get query parameters from the URI with
request.query.<name of the parameter>
, the second mentioned solution wasrequest.params.<name of the parameter>
and with this you can get the path variables.