简体   繁体   English

节点JS HTTP GET请求正文

[英]Node JS HTTP GET request body

I am using the express module in node.js and I am trying to read the body of an HTTP GET request and send an answer back to the user based on the content of the body. 我正在使用node.js中的express模块​​,并且试图读取HTTP GET请求的主体,并根据主体的内容将答案发送回用户。 I am new to node.js and this is not working for me. 我是node.js的新手,因此对我不起作用。

I know I should be using HTTP POST for this (and it works when I am using post), but the server I am trying to mimic in node.js uses GET, so I have to stick to it. 我知道我应该为此使用HTTP POST(并且当我使用post时它可以工作),但是我试图在node.js中模仿的服务器使用GET,所以我必须坚持下去。 Is there a way to do this in node.js? 有没有办法在node.js中做到这一点? Thanks! 谢谢!

Here is a sample code I did. 这是我做的示例代码。 When I use app.post, I see the logs for the body, but when I use app.get, I see no logs at all! 当我使用app.post时,我看到了正文的日志,但是当我使用app.get时,我根本看不到任何日志!

app.get('/someURL/', function(req, res){

  var postData = '';

  req.on('data', function(chunk) {
    console.log("chunk request:");
    console.log(chunk);
    postData += chunk;
  });

  req.on('end', function() {
    console.log("body request:");
    console.log(postData);
  });

  //Manipulate response based on postData information

  var bodyResponse = "some data based on request body"

  res.setHeader('Content-Type', 'application/json;charset=utf-8');
  res.setHeader('Content-Length', bodyResponse.length);

  res.send(bodyResponse);
};

The version of Node's HTML parser you are using does not support bodies in GET requests. 您使用的Node的HTML解析器版本不支持GET请求中的主体。 This feature is available on the latest unstable v0.11 branch. 最新的不稳定v0.11分支上提供了此功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM