问题描述
以下是一个示例(Express 3)中间件设置,该设置在全球范围内对我有用:
Here's an example (Express 3) middleware setup thats worked for me globally:
app.configure(function () {
app.use(express.static(__dirname + "/public"));
app.use(express.bodyParser({
keepExtensions: true,
limit: 10000000, // set 10MB limit
defer: true
}));
//... more config stuff
}
出于安全原因,我不想要以便在除/upload
之外的其他路由上允许500GB以上的帖子,因此我试图找出如何指定特定路由的限制,而不是在中间件中进行全局限制.
For security reasons, I don't want to allow 500GB+ posts on routes other than /upload
, so I'm trying to figure out how to specify the limit on specific routes, rather than globally in the middleware.
我知道bodyParser()中的多部分中间件已经嗅出了内容类型,但我想进一步限制它.
I know the multipart middleware in bodyParser() already sniffs out content types, but I want to limit it even further.
这似乎在快递3中无效:
This does not seem to work in express 3:
app.use('/', express.bodyParser({
keepExtensions: true,
limit: 1024 * 1024 * 10,
defer: true
}));
app.use('/upload', express.bodyParser({
keepExtensions: true,
limit: 1024 * 1024 * 1024 * 500,
defer: true
}));
当我尝试在 upload
URL上上传3MB文件时,出现错误 Error:请求实体太大
.
I get an error Error: Request Entity Too Large
when I try to upload a 3MB file on the upload
URL.
您如何正确执行此操作?
How do you do this correctly?
推荐答案
使用 app.use()
时只需指定可选的路径选项.
Just specify the optional path option when using app.use()
.
app.use('/', express.bodyParser({
keepExtensions: true,
limit: 1024 * 1024 * 10,
defer: true
}));
app.use('/upload', express.bodyParser({
keepExtensions: true,
limit: 1024 * 1024 * 1024 * 500,
defer: true
}));
这篇关于如何将bodyParser上传限制设置为特定路由,而不是在中间件中全局设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!