express
express copied to clipboard
timeout abnormal
when i use node:18.13.0 and express:4.18.2 to upload file, Uploading will fail between 5 minutes and 6 minutes. Firstly, response will emit "close". Secondly, request will be aborted.
here is my test code
const app = express();
app.post('/_/file/upload/put_file', (req: Request, res: Response) => { const fileStream = req.pipe(fs.createWriteStream('afse'));
fileStream.on('finish', () => {
res.send('success');
});
fileStream.on('error', (err: Error) => {
res.status(500).send('failed');
});
req.on('error', (err: Error) => {
console.log(`${new Date()} req err ${err}`);
});
req.on('close', () => {
console.log(`${new Date()} req close`);
});
res.on('error', (err: Error) => {
console.log(`${new Date()} res err ${err}`);
});
res.on('close', () => {
console.log(`${new Date()} res close`);
}); }); const port = 4000;
app.listen(port, () => {
console.log(${port}
);
});
i try to set request timeout and response timeout , but it is not useful
app.post('/_/file/upload/put_file', (req, res) => {
const fileStream = fs.createWriteStream('afse');
req.pipe(fileStream);
fileStream.on('finish', () => {
res.send('success');
});
fileStream.on('error', (err) => {
console.error(`${new Date()} File stream error: ${err}`);
res.status(500).send('failed');
});
req.on('error', (err) => {
console.error(`${new Date()} Request error: ${err}`);
});
req.on('close', () => {
console.log(`${new Date()} Request closed`);
fileStream.destroy();
});
res.on('error', (err) => {
console.error(`${new Date()} Response error: ${err}`);
fileStream.end();
});
res.on('close', () => {
console.log(`${new Date()} Response closed`);
});
});
Try this
i think these changes will help and ensure that the file stream is properly closed and that errors are handled more gracefully, reducing the chances of upload failures. if will not work please specifie more info and info. about the content or format. which you trying to upload .