Search code examples
javascriptnode.jsnode-http-proxy

Get request body for post operation


I use the http module and I need to get the req.body currently I try with the following without success .

http.createServer(function (req, res) {

console.log(req.body);

this return undfiend ,any idea why? I send via postman some short text...


Solution

  • req.body is a Express feature, as far as I know... You can retrieve the request body like this with the HTTP module:

    var http = require("http"),
    server = http.createServer(function(req, res){
      var dataChunks = [],
          dataRaw,
          data;
    
      req.on("data", function(chunk){
        dataChunks.push(chunk);
      });
    
      req.on("end", function(){
        dataRaw = Buffer.concat(dataChunks);
        data = dataRaw.toString();
    
        // Here you can use `data`
        res.end(data);
      });
    });
    
    server.listen(80)