multer
multer copied to clipboard
404 error on response but correctly uploaded to folder
Hello, I am trying to get the image url path using the Multer package when a user sends a file through a form. I have this setup and the image makes its way into the correct images folder in my public directory, but I get a 404 response back on the request and no post is created. Any help MUCH appreciated! Thanks!
// image uploading
const multer = require('multer')
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './public/images');
},
filename: function (req, file, cb) {
cb(null, new Date().toISOString() + 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
});
// create a post
router.post('/', upload.single('post_image'), withAuth, (req, res) => {
Post.create({
title: req.body.title,
post_url: req.body.post_url,
text: req.body.text,
post_image: req.file.path,
user_id: req.session.user_id
})
.then((dbPostData) => res.send(dbPostData))
.catch((err) => {
console.log(err);
res.status(500).json(err);
});
});
// on my main app.js I have
app.use(express.static(path.join(__dirname, 'public')));
the post request response