Node.js:如何限制HTTP请求大小和上传文件大小? [英] Node.js: how to limit the HTTP request size and upload file size?
问题描述
我想限制HTTP请求大小。假设有人向我发送超过2 MB的HTTP请求,那么我立即停止请求。我看着代码,我想如果我改变核心,我可以做到。但是,有没有办法设置一个 max_request_size
或这样的东西?
这与我的第二个问题我正在使用快递从 req.files
获取上传的文件。一旦文件大小超过某个文件大小,是否有办法停止将文件写入 / tmp
文件夹(这是默认的上传操作)?
只是更新(07-2014),因为我无法添加评论:
如上所述,较新的Express版本已弃用使用限制
中间件,现在将其作为 内置选项 为 BodyParser
中间件
var express = require('express')
var bodyParser = require('body-parser')
var app = express()
app.use(bodyParser.json({limit:'5mb'}))
I'm using Node.js and express.
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?
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?
Just an update (07-2014), as I'm not able to add comments:
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请求大小和上传文件大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!