Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js: how to limit the HTTP request size and upload file size?

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?

like image 603
murvinlai Avatar asked Jan 29 '12 00:01

murvinlai


People also ask

Does node js server block HTTP requests?

Your code is non-blocking because it uses non-blocking I/O with the request() function. This means that node. js is free to service other requests while your series of http requests is being fetched.


1 Answers

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' })) 
like image 147
Gaston Avatar answered Sep 23 '22 03:09

Gaston