File uploads are an essential feature in many web applications, whether for user profile images, documents, or other files.
In Node.js, file handling and uploads are commonly achieved using the Express
framework along with specialized middleware, such as Multer
, which simplifies handling multipart/form-data
, the encoding type used for file uploads. Working with file uploads involves setting up an API endpoint to accept files, processing and validating those files, and then storing them securely.
To handle file uploads, we start by creating a basic Express
server and installing Multer
, a middleware that parses form-data requests.
npm install express multer
const express = require('express');
const app = express();
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
We need to configure Multer
as middleware to handle incoming file data.
const multer = require('multer');
const upload = multer({ dest: 'uploads/' }); // 'dest' specifies the folder to store uploaded files
In this example, Multer
saves uploaded files to the uploads
directory, which we will create in our project’s root folder.
To upload a single file, we create a route in Express
where Multer
will process the request.
app.post('/upload', upload.single('file'), (req, res) => {
if (req.file) {
res.send(`File uploaded successfully: ${req.file.filename}`);
} else {
res.status(400).send("No file uploaded.");
}
});
upload.single('file')
middleware processes the incoming file with the name file
in the form data.req.file
holds metadata about the uploaded file (e.g., filename, path).By default, files are saved to the destination directory defined in Multer
, but you can customize this storage location.
Output: On successfully uploading a file, the response will display:File uploaded successfully: <generated filename>
To handle multiple file uploads, we modify the upload
middleware to accept an array of files.
app.post('/upload-multiple', upload.array('files', 5), (req, res) => {
if (req.files) {
res.send(`Uploaded ${req.files.length} files successfully.`);
} else {
res.status(400).send("No files uploaded.");
}
});
upload.array('files', 5)
accepts up to 5 files with the form field name files
.req.files
contains an array of file objects, each with its metadata.File validation is critical to avoid handling unwanted or malicious files. With Multer
, you can specify file type and size limits.
const fileFilter = (req, file, cb) => {
// Accept only images
if (file.mimetype.startsWith('image/')) {
cb(null, true);
} else {
cb(new Error('Only images are allowed!'), false);
}
};
const uploadWithValidation = multer({
dest: 'uploads/',
fileFilter: fileFilter,
limits: { fileSize: 1024 * 1024 } // 1MB limit
});
image/
(e.g., image/png
, image/jpeg
) are accepted.Offloading files to cloud storage allows scalable storage solutions. Using AWS S3 as an example, we configure Multer
to store files directly in S3.
npm install aws-sdk multer-s3
const AWS = require('aws-sdk');
const multerS3 = require('multer-s3');
AWS.config.update({ region: 'us-east-1' });
const s3 = new AWS.S3();
const uploadS3 = multer({
storage: multerS3({
s3: s3,
bucket: 'my-bucket',
key: function (req, file, cb) {
cb(null, Date.now().toString() + '-' + file.originalname);
}
})
});
For large files, you may use streams to process data as it’s uploaded rather than waiting until the entire file is received.
// Upload Profile Picture
app.post('/profile-picture', upload.single('picture'), (req, res) => {
if (req.file) {
// Save file info to database (pseudo-code)
saveProfilePicture(req.user.id, req.file.path);
res.send("Profile picture uploaded successfully!");
} else {
res.status(400).send("No picture uploaded.");
}
});
// Get Profile Picture
app.get('/profile-picture/:userId', (req, res) => {
const picturePath = getProfilePicture(req.params.userId);
res.sendFile(picturePath);
});
Handling file uploads in Node.js is a common requirement and can be effectively managed with Express and Multer. In this chapter, we explored various scenarios, from single and multiple file uploads to cloud storage. Happy Coding!❤️