Ошибка:
Error: ENOENT: no such file or directory, open
В корне проекта должна быть папка images, она у меня есть, но все равно эта ошибка, что не так?
Middleware file:
const multer = require('multer');
const storage = multer.diskStorage({
destination(req, file, cb) {
cb(null, 'images');
},
filename(req, file, cb) {
cb(null, new Date().toISOString() + '-' + file.originalname);
}
});
const allowedTypes = ['image/png', 'image/jpg', 'image/jpeg', 'image/gif'];
const fileFilter = (req, file, cb) => {
if (allowedTypes.includes(file.mimetype)) {
cb(null, true);
} else {
cb(null, false);
}
}
module.exports = multer({
storage, fileFilter
})
server:
const fileMiddleware = require('./middlewares/file.js');
app.use(fileMiddleware.single('img'));
route:
router.post('/:id/user-img', async(req, res) => {
try {
const userFind = await User.findById(req.params.id);
const toChange = {
firstName: userFind.firstName
}
if (req.file) {
toChange.img = req.file.path;
}
Object.assign(userFind, toChange);
await userFind.save();
res.redirect('/profile/'+userFind._id);
} catch (e) {
console.log(e.message);
}
})
html (pug):
form.profile__change-photo.block-hidden(
action="/profile/"+ user._id +"/user-img"
method="POST" enctype="multipart/form-data"
)
input(type="file" name="img" required)
input(type="submit" value="Load")