I'm using the multer package to let visitors upload their images. Everything works fine, but in the end, the path to the image is something like this:
"/images\the-picture.jpg "
Even in the database (I'm using MongoDB), the path is saved like this:
imageurl: images\the-picture.jpg
I'm using this middleware to give the multer the proper filter:
const fileStorage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, "images/");
},
filename: (req, file, cb) => {
cb(null, Math.round(Math.random() * 100000) + "-" + file.originalname);
},
});
const fileFilter = (req, file, cb) => {
if (
file.mimetype === "image/png" ||
file.mimetype === "image/jpg" ||
file.mimetype === "image/jpeg"
) {
cb(null, true);
} else {
cb(null, false);
}
};
app.use(multer({ storage: fileStorage, fileFilter: fileFilter }).single("image"));
And in the controller I simply get the path to file:
const imageurl = image.path
Do I need to fix this URL problem or I can ignore this?