how to use multer to upload image nodejs 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: express multer
$ npm install --save multer
var express = require("express");
var multer = require('multer');
var upload = multer({dest:'uploads/'});
Example 3: multer()
var multer = require('multer');
var upload = multer({dest:'uploads/'});