測試用的Echo Server。返回請求的header和rawbody

方健發表於2014-10-08
var connect = require('connect')
var http = require('http')
var bodyParser = require('body-parser')
var app = connect()

app.use(function(req, res, next) {
  req.rawBody = '';
  req.setEncoding('utf8');

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

  req.on('end', function() {
    next();
  });
});

//app.use(bodyParser.urlencoded());
app.use(function(req, res){
  console.log(JSON.stringify(req.headers, null, 2));
  console.log(req.rawBody);
  res.end(req.rawBody);
})

//create node.js http server and listen on port
http.createServer(app).listen(1337) 

相關文章