如何在Node.js上的Express.js中获取GET(查询字符串)变量?

是否可以像$_GET在PHP中一样在Node.js中获取查询字符串中的变量

我知道在Node.js中,我们可以在请求中获取URL。有没有获取查询字符串参数的方法?

逆天猿理查德2020/03/13 17:48:32

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 was request.params.<name of the parameter> and with this you can get the path variables.

飞云Stafan2020/03/13 17:48:32

You can use

request.query.<varible-name>;
西里老丝古一2020/03/13 17:48:32

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