问题描述
我正在使用 Node.js 和 express.
I'm using Node.js and express.
我想限制 HTTP 请求的大小.假设有人向我发送超过 2 MB 的 HTTP 请求,我会立即停止该请求.我查看了代码,我认为如果我更改核心,我可以做到.但是,有没有办法设置 max_request_size
或类似的东西?
I would like to limit the HTTP request size. Let's say, if someone sends me a HTTP request more than 2 MB then I stop the request right away. I looked at the code and I think if I change the core, I can do it. However, is there a way to set a max_request_size
or soemthing like that?
这与我的第二个问题有关.我正在使用 express 从 req.files
获取上传的文件.有没有办法在文件大小超过特定文件大小时停止将文件写入 /tmp
文件夹(这是默认上传行为)?
It is kind of related to my second question. I'm using express to get an uploaded file from req.files
. Is there a way to stop writing the file to the /tmp
folder (which is the default upload behavior) as soon as the file size exceeds a certain file size?
推荐答案
只是更新 (07-2014),因为我无法添加评论:
Just an update (07-2014), as I'm not able to add comments:
如上所述,较新的 Express 版本已弃用 limit
中间件,现在将其作为 内置选项,用于 BodyParser
中间件:
As correctly noted above, newer Express versions have deprecated the use of the limit
middleware and now provide it as a built-in option for the BodyParser
middleware:
var express = require('express')
var bodyParser = require('body-parser')
var app = express()
app.use(bodyParser.json({ limit: '5mb' }))
这篇关于Node.js:如何限制 HTTP 请求大小和上传文件大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!