Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Middleware doesn't prevent execution main function when file too large #238

Closed
shazgames1 opened this issue Jul 30, 2020 · 8 comments
Closed

Comments

@shazgames1
Copy link

router.post(
  "/upload",
  fileUpload({
    limits: { fileSize: 50 * 1024 * 1024 },
    files: 1, 
    abortOnLimit: true,
    limitHandler: (req, res, next) => {
      res.status(400).json({ error: true, message: "Request Entity Too Large" })
    },
  }),
  UploadFile
)

I getting errors like Cannot set headers after they are sent to the client from UploadFile function because limitHandler function has been called and responce already sent to client. How to prevent it or it's bug?

@PhilHannent
Copy link

I'm not the author but looking at the code you can see that the abortOnLimit is going to send a status, which would conflict with your response and end up with the message you are seeing:

if (options.abortOnLimit) {

@shazgames1
Copy link
Author

I'm not the author but looking at the code you can see that the abortOnLimit is going to send a status, which would conflict with your response and end up with the message you are seeing:

if (options.abortOnLimit) {

So then it should call the next function after error handler function

@imjordanxd
Copy link

imjordanxd commented Oct 15, 2020

I am also facing this problem. The response code of 413 is being sent; however, the next piece of middleware is being called. This seems to happen when files being uploaded are very close to the maximum upload size. The file limit and end event are both being emitted.

sliterok added a commit to sliterok/express-fileupload that referenced this issue Oct 28, 2020
Also allowed limitHandler and abortOnLimit to exist simultaneously without headers already sent error.
Solves richardgirges#238.
@sliterok
Copy link

I am also facing this problem. The response code of 413 is being sent; however, the next piece of middleware is being called. This seems to happen when files being uploaded are very close to the maximum upload size. The file limit and end event are both being emitted.

I guess the issue is somehow tied to sending files with speeds close to reading from disk. Anyway, should be fixed with PR.

@imjordanxd
Copy link

any updates on this?

@israelhuss
Copy link

I'm having the same issue, any solution or workaround?

@sliterok
Copy link

I'm having the same issue, any solution or workaround?

Check my PR #254 just above, it should still work

@RomanBurunkov
Copy link
Collaborator

Fixed in 1.4.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants