File upload functionality is widely used in web applications.. Whether it’s a profile picture, documents, or videos, handling file uploads efficiently is important. In Node.js, there are popular libraries like Multer and Busboy that make this task easier. In this post, we will discuss both and show simple examples.
1. Why Use Multer or Busboy?
Node.js itself can handle file uploads, but it’s not convenient for handling multipart/form-data (the type used in HTML forms for files). Libraries like Multer and Busboy help you:
- Parse incoming file data
- Save files to disk or memory
- Handle multiple files easily
- Avoid server crashes due to large files
2. File Upload Using Multer
Multer is a middleware for Express that makes file uploading simple.
Installation
npm install express multer
Example: Single File Upload
const express = require(‘express’); const multer = require(‘multer’); const app = express(); // Configure storage const storage = multer.diskStorage({ destination: (req, file, cb) => { cb(null, ‘uploads/’); // folder to save files }, filename: (req, file, cb) => { cb(null, Date.now() + ‘-‘ + file.originalname); } }); const upload = multer({ storage: storage }); // Single file upload app.post(‘/upload’, upload.single(‘myFile’), (req, res) => {
res.send(‘File uploaded successfully: ‘ + req.file.filename);
});
app.listen(3000, () => console.log(‘Server running on port 3000’));
Explanation:
- upload.single(‘myFile’) handles a single file with the field name myFile.
- Multer stores the file in the uploads/ folder with a timestamp added to its name.
Example: Multiple Files Upload
});
- upload.array(‘photos’, 5) allows uploading up to 5 files with the field name photos.
3. File Upload Using Busboy
Busboy is another module for handling file uploads. It’s more low-level compared to Multer but gives more control.
Installation
npm install busboy
Example: Upload File
const express = require(‘express’); const Busboy = require(‘busboy’); const fs = require(‘fs’); const path = require(‘path’); const app = express();
app.post(‘/upload-busboy’, (req, res) => {
const busboy = new Busboy({ headers: req.headers });
busboy.on(‘file’, (fieldname, file, filename) => {
const saveTo = path.join(__dirname, ‘uploads’, filename);
file.pipe(fs.createWriteStream(saveTo));
});
busboy.on(‘finish’, () => {
res.send(‘File uploaded successfully using Busboy!’);Uploading Files in Node.js: Multer vs Busboy
});
req.pipe(busboy);
});
app.listen(3000, () => console.log(‘Server running on port 3000’));
Explanation:
- 4Busboy streams the file directly to disk using file.pipe.
- It gives more control over how files are processed while uploading.
4. Multer vs Busboy
#google_vignette
| Feature | Multer | Busboy |
| Ease of Use | Very simple, beginner-friendly | More control, but more code |
| File Storage | Disk or memory | Disk, memory, or streaming |
| Multiple Files | Easy with .array() | Manual handling required |
| Control | Limited | High control over streams |
Tip: Use Multer if you want simplicity. Use Busboy if you need streaming large files or custom processing.
5. Important Tips
- Create an uploads folder before saving files.
- Limit file size to prevent server crashes.
- Validate file type to prevent uploading dangerous files.
- Handle errors properly using middleware.
Summary:
File uploading in Node.js is easy with Multer or Busboy. Multer is great for beginners or simple apps, while Busboy is useful for advanced use cases with streaming large files. Start with Multer if you are new to Node.js and explore Busboy as your app grows.



