Expressjs原始的身体
如何访问expressjs给我的请求对象的原始体?
var express = require('./node_modules/express'); var app = express.createServer(); app.post('/', function(req, res) { console.log(req.body); //says 'undefined' }); app.listen(80);
默认express
不会缓冲数据,除非您添加中间件来这样做。 简单的解决scheme是遵循@ Stewe的答案中的例子,它将自己连接所有的数据。 例如
var concat = require('concat-stream'); app.use(function(req, res, next){ req.pipe(concat(function(data){ req.body = data; next(); })); });
这个缺点是你现在已经将所有POST正文内容作为一个连续的块移动到RAM中,这可能不是必需的。 另一个值得考虑的选项,取决于需要在后文中处理多less数据,而不是将数据作为stream处理。
例如,对于XML,您可以使用支持parsingXML的XMLparsing器,因为它以块的forms出现。 一个这样的parsing器就是XML Stream 。 你做这样的事情:
var XmlStream = require('xml-stream'); app.post('/', function(req, res) { req.setEncoding('utf8'); var xml = new XmlStream(req); xml.on('updateElement: sometag', function(element) { // DO some processing on the tag }); xml.on('end', function() { res.end(); }); });
像这样的东西应该工作:
var express = require('./node_modules/express'); var app = express.createServer(); app.use (function(req, res, next) { var data=''; req.setEncoding('utf8'); req.on('data', function(chunk) { data += chunk; }); req.on('end', function() { req.body = data; next(); }); }); app.post('/', function(req, res) { console.log(req.body); }); app.listen(80);
在bodyParser中间件之前放置下面的中间件。 它会收集request.rawBody中的原始数据,不会干扰bodyParser。
app.use(function(req, res, next) { var data = ''; req.setEncoding('utf8'); req.on('data', function(chunk) { data += chunk; }); req.on('end', function() { req.rawBody = data; next(); }); }); app.use(express.bodyParser());
使用bodyParser.raw()
中间件将把原始体放入req.body
app.use(bodyParser.text({type: '*/*'}));
如果您想限制处理原始内容到某些路线或发布内容types,您也可以这样做。
app.use('/paths/to/save/raw/body/*', bodyParser.text({type: 'text/plain'})); //this type is actually the default app.use(bodyParser.json()); app.use(bodyParser.urlencoded({extended: true}));
注意:这个答案是针对节点v0.12.7,express 4.13.2和body-parser 1.13.3进行testing的。
所以,如果将content-type
设置为以下任何一种,那么Express的bodyParser似乎只parsing传入的数据:
-
application/x-www-form-urlencoded
-
application/json
-
multipart/form-data
在所有其他情况下,它甚至不会读取数据。
你可以换行号 92的express / node_modules / connect / lib / middleware / bodyParser.js from
} else { next(); }
至:
} else { var data=''; req.setEncoding('utf8'); req.on('data', function(chunk) { data += chunk; }); req.on('end', function() { req.rawBody = data; next(); }); }
然后,从你的代码中读取req.rawBody
。
如果您在上述解决scheme干扰正常发布请求时遇到问题,则可能会有所帮助:
app.use (function(req, res, next) { req.rawBody = ''; req.setEncoding('utf8'); req.on('data', function(chunk) { req.rawBody += chunk }); });
更多信息及来源: https : //github.com/visionmedia/express/issues/897#issuecomment-3314823
app.use(bodyParser.json({ verify: function (req, res, buf, encoding) { req.rawBody = buf; } })); app.use(bodyParser.urlencoded({ extended: false, verify: function (req, res, buf, encoding) { req.rawBody = buf; } }));
如果你想把身体作为一个缓冲区:
var rawParser = function(req, res, next) { var chunks = []; req.on('data', function(chunk) { chunks.push(chunk) }); req.on('end', function() { req.body = Buffer.concat(chunks); next(); }); }
要么
var rawParser = bodyParser.raw({type: '*/*'});
接着:
app.put('/:name', rawParser, function(req, res) { console.log('isBuffer:', Buffer.isBuffer(req.body)); })
或所有路线:
app.use(bodyParser.raw({type: '*/*'}));
请小心其他的答案,因为如果你还想支持json,urlencoded等,它们将无法正常使用bodyParser。为了使它与bodyParser一起工作,你应该限制你的处理器只注册在Content-Type
头上s)你关心,就像bodyParser本身一样。
要将具有Content-Type: "text/xml"
的请求的原始内容转换为req.rawBody
您可以:
app.use(function(req, res, next) { var contentType = req.headers['content-type'] || '' , mime = contentType.split(';')[0]; if (mime != 'text/xml') { return next(); } var data = ''; req.setEncoding('utf8'); req.on('data', function(chunk) { data += chunk; }); req.on('end', function() { req.rawBody = data; next(); }); });