1

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!

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

express.limit() has been deprecated. I get this error if I use it: "Error: Most middleware (like limit) is no longer bundled with Express and must be installed separately. Please see github.com/senchalabs/connect#middleware. "
1

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

2 Comments

Hmm.. Even if I set app.use(express.limit(1)) (1 byte) uploaded file is flushed to disk. I thought that limit will destroy the request if content-length is bigger than 1 byte. EDIT: OK. Everything works as expected. limit middleware has to placed before bodyParser()
@250r : Link is dead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.