Home > database >  Write logic to wrap usage of Multer middlware (Express.js)
Write logic to wrap usage of Multer middlware (Express.js)

Time:02-24

I'm using the multer middlware to upload files like so:

import multer from 'multer';

const storage = multer.diskStorage({
    destination: (req, file, callback) => {
    callback(null, './');
},
    filename: (req, file, callback) => {
    callback(null, `${Date.now()}${file.originalname}`);
}
});

const uploadFile = multer({ storage });

export default uploadFile.single('file');

And the route I'm using the middlware from:

router.post('/upload--file', uploadFile);

The issue is, I want to to add some logic before saving the file, after saving it etc.

How should I do it? I'm confused as I can't edit the middleware itself obviously.

CodePudding user response:

This solution is for error handling on multer but should answer your question on how to wrap the multer middleware to add your logic before the multer upload.

Link to solution

  • Related