Complete the code to import the middleware for handling file uploads.
const multer = require('[1]');
The multer package is used to handle file uploads in Express.
Complete the code to set the destination folder for uploaded files.
const upload = multer({ dest: '[1]' });The dest option specifies the folder where uploaded files are saved. Usually, a folder like uploads/ is used.
Fix the error in the route to handle single file upload with field name 'avatar'.
app.post('/profile', upload.[1]('avatar'), (req, res) => { res.send('File uploaded'); });
The single method handles uploading a single file with the given field name.
Fill both blanks to access the uploaded file's original name and size inside the route handler.
app.post('/upload', upload.single('file'), (req, res) => { const name = req.file.[1]; const size = req.file.[2]; res.send(`Uploaded file ${name} with size ${size} bytes`); });
The originalname property holds the original file name. The size property holds the file size in bytes.
Fill all three blanks to create a route that saves a single uploaded file and sends back its stored filename, mimetype, and size.
app.post('/file', upload.[1]('file'), (req, res) => { const storedName = req.file.[2]; const type = req.file.[3]; const size = req.file.size; res.json({ storedName, type, size }); });
The single method handles one file upload. The filename is the name given by multer when saving the file. The mimetype is the file's media type.