How to Handle File Uploads in NodeJS:
File uploads in NodeJS can be handled using middleware such as multer
or by directly parsing the incoming request stream. Here’s a basic example using multer
middleware:
const express = require('express');
const multer = require('multer');
const app = express();
const upload = multer({ dest: 'uploads/' });
app.post('/upload', upload.single('file'),
(req, res) => {
// Handle uploaded file
const file = req.file;
if (!file) {
return res.status(400)
.send('No file uploaded.');
}
res.send('File uploaded successfully.');
});
app.listen(3000,
() => {
console.log('Server is running on port 3000');
});
How to Manage File Uploads in Node ?
Handling file uploads in NodeJS involves receiving files sent from a client-side form, processing them, storing them on the server, or performing additional operations such as validation and manipulation.
Contact Us