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
How can I catch client error event? #5317
Comments
Here's an example of how you can configure timeout handling for your routes in Express: // Middleware to handle timeouts
app.use((req, res, next) => {
req.setTimeout(5000, () => {
const error = new Error('Request Timeout');
error.status = 408; // Request Timeout
next(error);
});
next();
});
// Your test route
app.post('/test', async (req: Request, res: Response) => {
// Simulating delay
await sleep(6000);
res.status(200).send();
});
// Error handling middleware
app.use((err, req, res, next) => {
if (res.headersSent) {
return next(err);
}
logger.error('Request Error:', err);
res.status(err.status || 500).json({ error: err.message });
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can I catch client error event like POST, PUT, etc?
I should catch client error like timeout, disconnect.
So I tried like below in log middleware.
With GET method, it worked
But with other method, it did not work..
This is my testcode in server. For test with timeout, I use sleep.
I'm using express 4.18 and node version 18
The text was updated successfully, but these errors were encountered: