I tried using multer as was specified in a tutorial. Then realized that it does not work in the same way now as it was used to be. So I checked the github page of multer and followed the documentation. However, my image though getting uploaded, the filename is not getting stored in the database.
Here is my code :
var express = require('express');
var router = express.Router();
var multer = require('multer');
var upload = multer({dest: './public/images/uploads/'});
router.post('/add', upload.single('mainimage'), function(req, res, next) {
//get form values
var title = req.body.title;
var category = req.body.category;
var body = req.body.body;
var author = req.body.author;
var date = new Date();
var mainImageName;
if(req.files && req.files.mainimage){
var mainImageNameOriginalName = req.files.mainimage.originalname;
mainImageName = req.files.mainimage.name;
var mainImageMime = req.files.mainimage.mimetype;
var mainImagePath = req.files.mainimage.path;
var mainImageExtension = req.files.mainimage.extension;
var mainImageSize = req.files.mainimage.size;
}else{
mainImageName = 'noimage.png';
}
// db insertion and yadi yadi yada
});
module.exports = router;
The image filename that is getting stored is noimage.png
though the image is getting uploaded. I defined the mainImageName
on the top to avoid hoisting