multer icon indicating copy to clipboard operation
multer copied to clipboard

404 error on response but correctly uploaded to folder

Open k-g-j opened this issue 4 years ago • 3 comments

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')));
Screen Shot 2022-03-06 at 10 32 03 PM the post request response

k-g-j avatar Mar 07 '22 03:03 k-g-j