Multer for JavaScript (Node.js): Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course
\n

Multer for JavaScript (Node.js): Complete Guide with Examples

\n\n

1. What It Is and Why It's Needed

\n

\n Multer is a popular middleware library for Node.js designed for handling multipart/form-data — the format used when uploading files via HTML forms. Unlike the standard body-parser module, which only works with JSON and URL-encoded data, Multer can parse multipart requests, extract files from them, and save them on the server (in memory or to disk). The library is built on top of the busboy module and provides a convenient API for configuring paths, file names, type filtering, and size limits.\n

\n

\n Why is Multer needed? In any web application where a user can upload an avatar, document, image, or video, it is necessary to process multipart/form-data. Express.js (the most popular framework for Node.js) does not have built-in support for this format, so Multer becomes an essential tool. It allows the developer to focus on business logic rather than low-level parsing of binary data.\n

\n

\n Multer supports uploading one or multiple files simultaneously, working with form fields, custom file name generation, filtering by MIME types and extensions, as well as limiting the size of uploaded data. Thanks to its flexible configuration, the library is suitable both for simple projects (e.g., avatar uploads) and for complex systems with cloud storage (via custom storage engines).\n

\n\n

2. Installation

\n

Installation is done via npm (Node Package Manager). Make sure you have Node.js installed (version 12 or higher).

\n
npm install multer
\n

After installation, the library will be available for import into your project.

\n\n

3. Quick Start — Minimal Working Example

\n

Let's create a simple Express.js server that accepts a single file upload via a form and saves it to the uploads/ folder.

\n\n

Step 1. Project Structure

\n
mkdir multer-example\ncd multer-example\nnpm init -y\nnpm install express multer\ntouch index.html\ntouch server.js
\n\n

Step 2. HTML Form (index.html)

\n
<!DOCTYPE html>\n<html lang="en">\n<head>\n    <meta charset="UTF-8">\n    <title>File Upload</title>\n</head>\n<body>\n    <h1>Upload a file</h1>\n    <form action="/upload" method="POST" enctype="multipart/form-data">\n        <input type="file" name="myFile" required>\n        <button type="submit">Submit</button>\n    </form>\n</body>\n</html>
\n\n

Step 3. Server (server.js)

\n
const express = require('express');\nconst multer = require('multer');\nconst path = require('path');\n\nconst app = express();\nconst PORT = 3000;\n\n// Multer configuration: files will be saved to the 'uploads/' folder\nconst storage = multer.diskStorage({\n    destination: (req, file, cb) => {\n        cb(null, 'uploads/');\n    },\n    filename: (req, file, cb) => {\n        // Generate a unique name: current date + original extension\n        const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);\n        cb(null, uniqueSuffix + path.extname(file.originalname));\n    }\n});\n\nconst upload = multer({ storage: storage });\n\n// Serve static HTML\napp.get('/', (req, res) => {\n    res.sendFile(__dirname + '/index.html');\n});\n\n// Handle file upload\napp.post('/upload', upload.single('myFile'), (req, res) => {\n    if (!req.file) {\n        return res.status(400).send('File not uploaded.');\n    }\n    res.send(`File "${req.file.originalname}" successfully uploaded. Size: ${req.f                    

Recommendations