Double backslash into path

Viewed 1074

I have using this the tuttorial of Academind how to load image using multer into node JavaScript rest service. About path destination folder I use this code:

const storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, './uploads/');
},
filename: function(req, file, cb) {
cb(null, file.originalname);
}
});

const fileFilter = (req, file, cb) => {
// reject a file
if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/png') {
cb(null, true);
} else {
cb(null, false);
}
};

const upload = multer({
storage: storage,
limits: {
fileSize: 1024 * 1024 * 5
},
fileFilter: fileFilter
});

But the path generated have double backslash that show as one backslash: uploads\2018-09-16T13-02-45.535Z1536685465dummy4.jpg How I can replace destination path with slash?

1 Answers

i had the same issue, and since i didn't find anything i did this as a work around

const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, './uploads/');
    },
    filename: function (req, file, cb) {
        cb(null, `${new Date().toISOString().replace(/:/g, '-')}${file.originalname.split(" ").join("_")}`);
    }
});

const user= new User({
            _id : new mongoose.Types.ObjectId(),
            name : req.body.name,
            image: req.file.destination + req.file.filename, 
        });
Related