question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

404 With Multiple Images in One Request

See original GitHub issue

I’m receiving a 404 Not Found error when I attempt to submit a form that has two images uploaded. Neither image is a required field, so when I upload one image (can be either) everything works correctly, and I do not get an error. The issue only occurs when I upload both in one request. Checking my uploads folder, when I get a 404, both images are correctly uploaded. Here is the code for multer and resizing:

const multerOptions = {
  storage: multer.memoryStorage(),
  fileFilter(req, file, next) {
    const isPhoto = file.mimetype.startsWith('image/');
    if (isPhoto) {
      next(null, true);
    } else {
      next({ message: 'That filetype isn\'t allowed!' }, false);
    }
  },
};

export const upload = multer(multerOptions).fields([
  { name: 'avatar' },
  { name: 'accolade' },
]);

export const resize = async (req, res, next) => {
  if (!req.files) {
    next();
    return;
  }
  Object.keys(req.files).forEach(async (file) => {
    const extension = req.files[file][0].mimetype.split('/')[1];
    req.body[file] = `${uuid.v4()}.${extension}`;
    const uploaded = await jimp.read(req.files[file][0].buffer);
    if (file === 'avatar') {
      await uploaded.resize(300, jimp.AUTO);
    } else if (file === 'accolade') {
      await uploaded.resize(30, jimp.AUTO);
    }
    await uploaded.write(`./public/uploads/${req.body[file]}`);
    next();
  });
};

I’m passing both functions to this route:

router.post(
  '/team-members/add/:id',
  authController.authCheck,
  userController.isAdmin,
  userController.upload,
  userController.resize,
  userController.validateUser,
  catchErrors(userController.addTeamMember),
);

The final method, addTeamMember fires correctly and the user is added, but I get a 404 not found and Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client. Any ideas?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
joshloftiscommented, Apr 23, 2018

Ugh. I found it. I included next() inside the forEach block in the resize method on accident. Moved it outside and everything works. Thanks!

1reaction
joshloftiscommented, Apr 23, 2018

Thanks again for responding!

I added the return statement, but unfortunately still getting the error when uploading two images at the same time.

I’ve added a question at SO, and will close this issue as it seems it probably isn’t an issue with this package from your responses. https://stackoverflow.com/questions/49982952/nodejs-multer-receiving-404-and-cannot-set-headers-when-uploading-multiple-fil

Read more comments on GitHub >

github_iconTop Results From Across the Web

link returning 404 for images, but the direct link to asset loads ...
In a webpage I am building, a few images are returning 404 errors, but the direct link to the images load them fine....
Read more >
All GET request for images are all HTTP 404 NOT FOUND
Every GET request to images are shown with the HTTP 404 NOT FOUND. I have been having this issue for a while now...
Read more >
Error 404: 4 Ways to Fix It - Hostinger
Error 404 is a response code, meaning the server could not locate the requested content. Check this article to learn 4 steps to...
Read more >
Getting 404 on image urls in bulk query - Shopify Community
I'm submitting a bulk query for images, and they are returned in the bulk file. It looks like we're able to access the...
Read more >
“The remote server returned an Error 404” or "HTTP request ...
Describes an issue that triggers a 404 error or "The HTTP request to ‎'https://mail. .com/EWS/mrsproxy.svc‎' has exceeded the allotted timeout" error when ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found