问题描述
这看起来应该是一个相当简单的问题,但我真的很难弄清楚如何解决它.
This seems like it should be a fairly simple question, but I'm having a really hard time figuring out how to approach it.
我正在使用 Node.js + Express 构建一个 Web 应用程序,我发现 express 公开的 connect BodyParser 在大多数情况下非常有用.但是,我希望对多部分表单数据 POSTS 进行更精细的访问 - 我需要将输入流通过管道传输到另一台服务器,并希望避免先下载整个文件.
I'm using Node.js + Express to build a web application, and I find the connect BodyParser that express exposes to be very useful in most cases. However, I would like to have more granular access to multipart form-data POSTS as they come - I need to pipe the input stream to another server, and want to avoid downloading the whole file first.
因为我使用的是 Express BodyParser,所以所有文件上传都会在它们到达我的任何函数之前自动解析并上传并使用request.files"可用.
Because I'm using the Express BodyParser, however, all file uploads are parsed automatically and uploaded and available using "request.files" before they ever get to any of my functions.
有没有办法为多部分表单数据帖子禁用 BodyParser,而不为其他所有内容禁用它?
Is there a way for me to disable the BodyParser for multipart formdata posts without disabling it for everything else?
推荐答案
当你输入app.use(express.bodyParser())
时,几乎每个请求都会经过bodyParser
code> 函数(执行哪个取决于 Content-Type
标头).
When you type app.use(express.bodyParser())
, almost each request will go through bodyParser
functions (which one will be executed depends on Content-Type
header).
默认情况下,支持 3 个标头 (AFAIR).你可以看到消息来源.您可以(重新)定义 Content-Type
的处理程序,如下所示:
By default, there are 3 headers supported (AFAIR). You could see sources to be sure. You can (re)define handlers for Content-Type
s with something like this:
var express = require('express');
var bodyParser = express.bodyParser;
// redefine handler for Content-Type: multipart/form-data
bodyParser.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
ed before express.bodyParser()
):
Things have changed in Express 3, so I'm sharing updated code from working project (should be app.use
ed before express.bodyParser()
):
var connectUtils = require('express/node_modules/connect/lib/utils');
/**
* Parses body and puts it to `request.rawBody`.
* @param {Array|String} contentTypes Value(s) of Content-Type header for which
parser will be applied.
* @return {Function} Express Middleware
*/
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);
};
};
还有一些伪代码,关于原始问题:
And some pseudo-code, regarding original question:
function disableParserForContentType(req, res, next) {
if (req.contentType in options.contentTypes) {
req._body = true;
next();
}
}
这篇关于如何为文件上传禁用 Express BodyParser (Node.js)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!