如何在'nodejs'中查找请求参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18612342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to find request parameters in 'nodejs'
提问by codeofnode
when i sent a request to nodejs server,
当我向 nodejs 服务器发送请求时,
how can we find the parameters sent in the request query when request sent to nodejs server.
当请求发送到nodejs服务器时,我们如何找到请求查询中发送的参数。
req.param
req.params
req.query
all giving undefined.
都给未定义。
also when i stringifyreqrequest it gives error :
也当我stringifyreq请求它给出错误:
Converting circular structure to JSON
How to find query parameters.
如何查找查询参数。
回答by Jazor
You can use the urlmodule:
您可以使用url模块:
$ npm install url
And then something like this:
然后是这样的:
var http = require("http");
var url = require("url");
http.createServer(function(req, res) {
var parsedUrl = url.parse(req.url, true); // true to get query as object
var queryAsObject = parsedUrl.query;
console.log(JSON.stringify(queryAsObject));
res.end(JSON.stringify(queryAsObject));
}).listen(8080);
console.log("Server listening on port 8080");
Test in your browser:
在浏览器中测试:
http://localhost:8080/?a=123&b=xxy
For POST requests you can use bodyParser.
对于 POST 请求,您可以使用bodyParser。

