如何在'nodejs'中查找請求參數

[英]how to find request parameters in 'nodejs'


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.

所有給出undefined。

also when i stringify req request it gives error :

當我對string請求進行stringify時它會給出錯誤:

Converting circular structure to JSON

How to find query parameters.

如何查找查詢參數。

1 个解决方案

#1


16  

You can use the url module:

您可以使用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。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/09/04/72046ba802ae293ab4aae83201d496d2.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com