npm install multer express
Then we will set up express and mutler
like creating a basic express app and configure Multer for file uploads
const express = require('express');
const multer = require('multer');
const path = require('path');
const app = express();
// Configure Multer storage
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'uploads/'); // Specify the folder for uploads
},
filename: (req, file, cb) => {
cb(null, Date.now() + path.extname(file.originalname)); // Append file extension and timestamp to file
}
});
// Initialize Multer
const upload = multer({ storage: storage });
// Define a route for file upload
app.post('/upload', upload.single('file'), (req, res) => {
try {
res.send({
message: 'File uploaded successfully',
file: req.file
});
} catch (error) {
res.status(400).send('File upload failed');
}
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});
then we will create the upload folder : - mkdir uploads
So , next
1. Open Postman.
2. Select POST request and enter http://localhost:3000/upload.
3. In the Body tab, select form-data.
4. Add a key named file and select File from the dropdown.
5. Choose a file from your system to upload.
6. Send.