当您键入时
app.use(express.bodyParser()),几乎每个请求都将通过
bodyParser函数(将执行哪个请求取决于
Content-Type标头)。
默认情况下,支持3个标头(AFAIR)。您可以确定来源。您可以使用以下方法来(重新)定义的处理程序
Content-Type:
var express = require('express');var bodyParser = express.bodyParser;// redefine handler for Content-Type: multipart/form-databodyParser.parse('multipart/form-data') = function(req, options, next) { // parse request body your way; example of such action: // https://github.com/senchalabs/connect/blob/master/lib/middleware/multipart.js // for your needs it will probably be this: next();}更新。
Express 3发生了变化,因此我正在共享工作项目中的更新代码(应在 之前
app.use编辑): __
express.bodyParser()
var connectUtils = require('express/node_modules/connect/lib/utils');module.exports = function(contentTypes) { contentTypes = Array.isArray(contentTypes) ? contentTypes : [contentTypes]; return function (req, res, next) { if (req._body) return next(); req.body = req.body || {}; if (!connectUtils.hasBody(req)) return next(); if (-1 === contentTypes.indexOf(req.header('content-type'))) return next(); req.setEncoding('utf8'); // Reconsider this line! req._body = true; // Mark as parsed for other body parsers. req.rawBody = ''; req.on('data', function (chunk) { req.rawBody += chunk; }); req.on('end', next); };};还有一些关于原始问题的伪代码:
function disableParserForContentType(req, res, next) { if (req.contentType in options.contentTypes) { req._body = true; next(); }}


