Multer
Express middleware for multipart/form-data uploads with disk or memory storage, field limits, file filters, and multiple file handling.
Official Documentation ↗v2.5.0. Check the official docs for the latest API surface.Installation
npm install multerpnpm add multeryarn add multerMethods
No methods match this filter.
Multer parses multipart/form-data and exposes files to Express. It is a parser, not a complete upload security layer. Configure a storage destination, size and count limits, and a filter, then validate the file content and generate a safe storage name on the server.
Storage and limits
Disk storage writes files to the filesystem; memory storage buffers them in RAM and should only be used for small files. Set fileSize and files limits before the parser reads the whole body. The reverse proxy and application should enforce compatible limits so a large request is rejected at the edge.
Validate after upload
The client-provided filename and MIME type are untrusted. Generate a new name, validate the extension, inspect the file signature, and store uploads outside the web root or in object storage. Serve user files with a restrictive content type and disposition.
Errors
The following points are quoted verbatim from the official documentation (https://expressjs.com/en/resources/middleware/multer.html). Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.
- Optionally error out if more than maxCount files are uploaded. (https://expressjs.com/en/resources/middleware/multer.html)
- If any file upload is made, error with code LIMIT_UNEXPECTED_FILE will be issued. (https://expressjs.com/en/resources/middleware/multer.html)
- WARNING: Make sure that you always handle the files that a user uploads. (https://expressjs.com/en/resources/middleware/multer.html)
Security notes
Quoted from the official documentation (https://expressjs.com/en/resources/middleware/multer.html); this is not a complete threat model.
- This is exposed as file.originalname ; it does not change the destination folder, create directories, or sanitize the path for you. (https://expressjs.com/en/resources/middleware/multer.html)
- Never add multer as a global middleware since a malicious user could upload files to a route that you didn’t anticipate. (https://expressjs.com/en/resources/middleware/multer.html)
- Security Specifying the limits can help protect your site against denial of service (DoS) attacks. (https://expressjs.com/en/resources/middleware/multer.html)
FAQ
- Does Multer limit upload size by default?
- No. The default fileSize and file count limits are not a complete protection. Set explicit limits and enforce the same or a stricter limit in the application and reverse proxy.
- Is the mimetype reported by Multer trustworthy?
- No. It comes from the client. Use it only as an early filter, then inspect the saved file's signature and run malware scanning if uploads are public.
- Can I use the original filename for storage?
- Avoid it. The name can contain traversal sequences or conflicting characters. Generate a UUID or hash, store the original name as metadata, and validate the extension.
Report a problem with this API docOpens a pre-filled issue on GitHub.