2016-09-30 73 views
1

我有这个节点的http服务器。我通过的信息去了哪里?

/** 
* Module dependencies. 
*/ 

var http = require('http'), 
    fs = require('fs'), 
    cycle = require('./cycle.js'); 

/** 
* Create the server. 
*/ 

var server = http.createServer(function (req, resp) { 
    console.log(JSON.decycle(req)); 
    if ('GET' === req.method && '/images' === req.url.substr(0, 7) && 
      '.jpg' === req.url.substr(-4)) { 
     fs.stat(__dirname + req.url, function (err, stat) { 
      if (err || !stat.isFile()) { 
       res.writeHead(404); 
       res.end('Not Found'); 
       return; 
      } 
      serve(__dirname + req.url, 'application/jpg'); 
     }); 
    } else if ('GET' === req.method && '/' === req.url) { 
     serve(__dirname + '/index.html', 'text/html'); 
    } else { 
     resp.writeHead(404); 
     resp.end('Not found'); 
    } 

    function serve(path, type) { 
     resp.writeHead(200, {'Content-Type': type}); 
     fs.createReadStream(path).pipe(resp); 
    } 
}); 

/** 
* Listen. 
*/ 

server.listen(3000); 

(它使用Crockford的对象打印实用程序。https://github.com/douglascrockford/JSON-js/blob/master/cycle.js

当我与这个要求打它,我传递的信息项不会出现在该请求。为什么不?

RickH$ curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -H "Cache-Control: no-cache" -H "Postman-Token: 647062e3-bd38-edf0-3b3a-e56227daf94c" -d '&first_name=James&last_name=Taylor&city=Urbana&state=Illinois' "localhost:3000" 
RickH$ 

这是一个大型项目的简化版本。在较大的项目中,请求被传送到另一个服务器,该服务器根据请求的内容作出反应。所以我觉得我知道内容正在通过。但它在哪里?

后来----------------

/********* 
Version 2 of server callback function. This one shows the full request body. 
var server = http.createServer(function (req, resp) { 
    console.log(); 
    console.log('=========================%=========================='); 
    console.log(); 
    console.log(JSON.decycle(req)); 

    req.rawBody = ''; 
    req.setEncoding('utf8'); 

    req.on('data', function (chunk) { 
     req.rawBody += chunk; 
     // next(); 
    }); 

    req.on('end', function() { 
     console.log(); 
     console.log('~~~~~~~~~~~~~~~~~~~~~~~~~+~~~~~~~~~~~~~~~~~~~~~~~~~~'); 
     console.log(); 
     console.log(JSON.decycle(req)); 
     // next(); 
    }); 

    if ('GET' === req.method && '/images' === req.url.substr(0, 7) && 
      '.jpg' === req.url.substr(-4)) { 
     fs.stat(__dirname + req.url, function (err, stat) { 
      if (err || !stat.isFile()) { 
       res.writeHead(404); 
       res.end('Not Found'); 
       return; 
      } 
      serve(__dirname + req.url, 'application/jpg'); 
     }); 
    } else if ('GET' === req.method && '/' === req.url) { 
     serve(__dirname + '/index.html', 'text/html'); 
    } else { 
     resp.writeHead(404); 
     resp.end('Not found'); 
    } 

    function serve(path, type) { 
     resp.writeHead(200, {'Content-Type': type}); 
     fs.createReadStream(path).pipe(resp); 
    } 
}); 
*********/ 

回答

1

你需要下面的代码获取体(您的信息)的请求,

var data; 
req.on('data', function(chunk){ 
    data += chunk; 
}); 
req.on('end', function(){ 
    console.log(data); 
}); 
+0

你的想法完全对,摩尔达!我觉得知道我们如何到达这里很有用。这是上下文。 http://stackoverflow.com/questions/18710225/node-js-get-raw-request-body-using-express – user2171796