快速获取rawBody

您好,我试图从post中检索的东西,并需要来自请求的rawBody属性。 我怎样才能找回?

我尝试使用express.bodyParser()和我的后处理程序,我正在寻找req.rawBody,它是未定义的。

我甚至尝试过connect.bodyParser(),但是我仍然没有运气。 我正在为rawBody定义。

我正在阅读的stackoverflow网站上说,他们已经删除了rawBodyfunction,但提到这是一个快速修复,将其添加到我们自己的中间件文件。 我是一个新手,所以我不知道如何做到这一点。 以下是我的代码片段。

/** * Module dependencies. */ var express = require('express') , connect = require('connect') , routes = require('./routes') , user = require('./routes/user') , http = require('http') , path = require('path'); var app = express(); // all environments app.set('port', process.env.PORT || 3000); app.set('views', __dirname + '/views'); app.set('view engine', 'jade'); app.use(express.favicon()); app.use(express.logger('dev')); //app.use(express.bodyParser()); app.use(connect.bodyParser()); app.use(express.methodOverride()); app.use(app.router); app.use(express.static(path.join(__dirname, 'public'))); // development only if ('development' == app.get('env')) { app.use(express.errorHandler()); } app.get('/', routes.index); app.get('/users', user.list); /**custom stuff**/ app.post('/upload',function(req, res){ console.log(req.header('Content-Type')); console.log(req.header('Host')); console.log(req.header('User-Agent')); console.log(req.rawBody); console.log(req.body); res.send("<h1> Hello the response is "+req.body.username); }); /** end**/ http.createServer(app).listen(app.get('port'), function(){ console.log('Express server listening on port ' + app.get('port')); }); 

任何帮助,这是非常感谢。

谢谢。

你可以使用自己的中间件来做到这一点:

 app.use(function(req, res, next){ var data = ""; req.on('data', function(chunk){ data += chunk}) req.on('end', function(){ req.rawBody = data; next(); }) }) // Your route registration: app.get('/', function(){// whatever...}) app.post('/test', function(req, res){ console.log(req.rawBody); res.send("your request raw body is:"+req.rawBody); }) 

我又回来了:D。 阅读connect.bodyParser后,我发现:bodyParser只parsingMIMEtypes是application / json,application / x-www-form-urlencoded和multipart / form-data之一的数据。 所以我认为这是另一种方法,通常并不优雅,但是可以接受:当你尝试发送原始数据到服务器时,把MIMEtypes改成不同的东西。 由于你的问题是一个string,所以我selecttext / plain作为例子:

 // if the request's mime type is text/plain, read it as raw data var myRawParser = function(req, res, next){ req.rawData = ''; if(req.header('content-type') == 'text/plain'){ req.on('data', function(chunk){ req.rawData += chunk; }) req.on('end', function(){ next(); }) } else { next(); } } // ... app.use(myRawParser); app.use(express.bodyParser()); // ... // Here is my test route: app.post('/test', function(req, res){ console.log('Mime type is:'+req.header('content-type')); console.log('Raw data is:'+req.rawData); console.log('Body via bodyParser is:'); console.dir(req.body); res.send('Hello!'); }) 

我已经通过curltesting了它:

 $ curl -d 'test=hello' 127.0.0.1:3000/test // console result: Mime type is:application/x-www-form-urlencoded Raw data is: Body via bodyParser is: { test: 'hello' } 

和:

 $ curl -d 'test=hello' -H 'Content-Type:text/plain' 127.0.0.1:3000/test // console result: Mime type is:text/plain Raw data is:test=hello Body via bodyParser is: {} 

这实际上并不是将你的中间件集成到bodyParser,只是让它们一起工作。