I'm developing a simple app that will allow users to upload their content. I want to limit the file size which Express will accept. I know I can use
app.use(express.limit('2mb'));
but I want to change the limit dynamically. Certain users will have higher limits.
The best solution would be to FIRST check the content-length
header and THEN start to parse the upload if file size is below the limit but Express automatically saves file uploads to disk (/tmp directory). So if someone sends 1GB file, this file will be parsed and saved to disk even when I don't want to allow that.
Thanks in advance!
Add limit() before adding bodyParser(), so for example:
app.use(express.limit(100000));
app.use(express.bodyParser({uploadDir: '/tmp', keepExtensions:true}));
This way the uploaded file won't be saved in /tmp when it is too big.
Express basically uses connect's limit() middleware.
So you might want to write you own middleware instead of using the default limit().
You can start by looking at the source code for limit() here https://github.com/senchalabs/connect/blob/2.24.2/lib/middleware/limit.js#L40