richardgirges / express-fileupload

Simple express file upload middleware that wraps around busboy
MIT License
1.52k stars 261 forks source link

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

Closed shazgames1 closed 10 months ago

shazgames1 commented 4 years ago
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 commented 4 years ago

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:

https://github.com/richardgirges/express-fileupload/blob/64587932f7adb10e41702cadade6a873b05e6ca6/lib/processMultipart.js#L75

shazgames1 commented 4 years ago

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:

https://github.com/richardgirges/express-fileupload/blob/64587932f7adb10e41702cadade6a873b05e6ca6/lib/processMultipart.js#L75

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

imjordanxd commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

any updates on this?

israelhuss commented 1 year ago

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

sliterok commented 1 year ago

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

Check my PR https://github.com/richardgirges/express-fileupload/pull/254 just above, it should still work

RomanBurunkov commented 10 months ago

Fixed in 1.4.2