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

node.js Node.js

SamStafan十三

2020-03-13

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

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

第1497篇《如何在Node.js上的Express.js中获取GET(查询字符串)变量?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

3个回答
逆天猿理查德 2020.03.13

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.

飞云Stafan 2020.03.13

You can use

request.query.<varible-name>;
西里老丝古一 2020.03.13

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

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android