You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Pardon me if this has been asked a million time but It's not clear how to best handle timeouts. The following script will crash the server since res.send is called after the timeout. I could wrap res.send in an if !res.headersSent but it seems a little excessive to do that for every route. I'm not sure how to best handle this situation.
var express = require('express');
var timeout = require('connect-timeout');
var handleTimeout = function (req, res, next) {
if (!req.timedout) next();
};
var app = express()
.use(timeout(500))
.use(handleTimeout)
.get("/", function (req, res, next) {
setTimeout(function () {
// This causes "Error: Can't set headers after they are sent."
res.send({peaches:"yummy", turnips:"yucky"});
}, 1000);
})
.use(handleTimeout)
.listen(8888, function(){
console.log('Server started on port 8888');
});
Error:
Starting child process with 'node timeout.js'
Server started on port 8888
ServiceUnavailableError: Response timeout
at IncomingMessage.<anonymous> (/Users/jw/Desktop/timeout/node_modules/connect-timeout/index.js:69:8)
at IncomingMessage.emit (events.js:95:17)
at null._onTimeout (/Users/jw/Desktop/timeout/node_modules/connect-timeout/index.js:41:11)
at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)
http.js:689
throw new Error('Can\'t set headers after they are sent.');
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js:689:11)
at ServerResponse.header (/Users/jw/Desktop/timeout/node_modules/express/lib/response.js:718:10)
at ServerResponse.send (/Users/jw/Desktop/timeout/node_modules/express/lib/response.js:163:12)
at ServerResponse.json (/Users/jw/Desktop/timeout/node_modules/express/lib/response.js:249:15)
at ServerResponse.send (/Users/jw/Desktop/timeout/node_modules/express/lib/response.js:151:21)
at null._onTimeout (timeout/timeout.js:13:17)
at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)
Program node timeout.js exited with code 8
The text was updated successfully, but these errors were encountered:
I could wrap res.send in an if !res.headersSent but it seems a little excessive to do that for every route. I'm not sure how to best handle this situation.
There are two solutions:
You can do as you suggest: check if the request timed out once your timer fired.
When you set your timeout, also add a listener on the timeout event and then clear the timeout so your timer never even fires:
Pardon me if this has been asked a million time but It's not clear how to best handle timeouts. The following script will crash the server since
res.send
is called after the timeout. I could wrapres.send
in anif !res.headersSent
but it seems a little excessive to do that for every route. I'm not sure how to best handle this situation.Error:
The text was updated successfully, but these errors were encountered: