how to upload image using multer in node js code example
Example 1: Save image in node js with multer
const multer = require('multer');
const MIME_TYPES = {
'image/jpg': 'jpg',
'image/jpeg': 'jpg',
'image/png': 'png'
};
const storage = multer.diskStorage({
destination: (req, file, callback) => {
callback(null, 'images');
},
filename: (req, file, callback) => {
const name = file.originalname.split(' ').join('_');
const extension = MIME_TYPES[file.mimetype];
callback(null, name + Date.now() + '.' + extension);
}
});
module.exports = multer({storage: storage}).single('image');
Example 2: multer()
var multer = require('multer');
var upload = multer({dest:'uploads/'});