'express-fileupload limithandler crushing the server on redirect
const express = require('express');
const app = express();
const fu = require('express-fileupload');
const fs = require('fs');
app.listen(3000, () => console.log("Servidor activo en http://localhost:3000"));
app.use(express.json());
app.use(fu({
limits: {fileSize: 5 * 1024 * 1024},
limitHandler: (req, res) => {
return res.redirect("/collage")
}
}));
This code gives me the following output in the server and makes the redirect but shutting down the server.
node:_http_outgoing:576
throw new ERR_HTTP_HEADERS_SENT('set');
^
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at new NodeError (node:internal/errors:371:5)
at ServerResponse.setHeader (node:_http_outgoing:576:11)
at ServerResponse.header (D:\Documentos\Curso Bootcamp JS\Desafios\Modulo8\M8U2C2_Collage\node_modules\express\lib\response.js:794:10)
at ServerResponse.location (D:\Documentos\Curso Bootcamp JS\Desafios\Modulo8\M8U2C2_Collage\node_modules\express\lib\response.js:915:15)
at ServerResponse.redirect (D:\Documentos\Curso Bootcamp JS\Desafios\Modulo8\M8U2C2_Collage\node_modules\express\lib\response.js:953:18)
at D:\Documentos\Curso Bootcamp JS\Desafios\Modulo8\M8U2C2_Collage\index.js:50:17
at D:\Documentos\Curso Bootcamp JS\Desafios\Modulo8\M8U2C2_Collage\node_modules\express-fileupload\lib\utilities.js:64:52
at WriteStream.<anonymous> (D:\Documentos\Curso Bootcamp JS\Desafios\Modulo8\M8U2C2_Collage\node_modules\express-fileupload\lib\utilities.js:220:5)
at WriteStream.emit (node:events:390:28)
at emitCloseNT (node:internal/streams/destroy:138:10) {
code: 'ERR_HTTP_HEADERS_SENT'
}
The route "/collage". And already have another redirect, just as simple, and it that works:
app.get("/collage", (req, res) => {
res.sendFile(`${__dirname}/collage.html`);
});
Where I am making that other "sent" that's crashing my server? Or how can I use the limithandler so when the file exceeds the limit, it redirects to another page? And it is for a bootcamp, so I can't use multer. Also I know that I can do it with another middleware and req.size, but would be nice to see how can I do it, or to know that it cannot be done.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|