Skip to content

Instantly share code, notes, and snippets.

@jish
Created December 14, 2018 23:08
Show Gist options
  • Save jish/60ba346a2ce145bbdef30f666f2f763a to your computer and use it in GitHub Desktop.
Save jish/60ba346a2ce145bbdef30f666f2f763a to your computer and use it in GitHub Desktop.
Express timeout
$ time curl -i "http://localhost:3000/never"
HTTP/1.1 503 Service Unavailable
X-Powered-By: Express
Content-Security-Policy: default-src 'self'
X-Content-Type-Options: nosniff
Content-Type: text/html; charset=utf-8
Content-Length: 704
Date: Fri, 14 Dec 2018 23:04:13 GMT
Connection: keep-alive
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>ServiceUnavailableError: Response timeout<br> &nbsp; &nbsp;at IncomingMessage.&lt;anonymous&gt; (/Users/josh/code/ecmascript/express_timeout/node_modules/connect-timeout/index.js:84:8)<br> &nbsp; &nbsp;at emitOne (events.js:116:13)<br> &nbsp; &nbsp;at IncomingMessage.emit (events.js:211:7)<br> &nbsp; &nbsp;at Timeout._onTimeout (/Users/josh/code/ecmascript/express_timeout/node_modules/connect-timeout/index.js:49:11)<br> &nbsp; &nbsp;at ontimeout (timers.js:482:11)<br> &nbsp; &nbsp;at tryOnTimeout (timers.js:317:5)<br> &nbsp; &nbsp;at Timer.listOnTimeout (timers.js:277:5)</pre>
</body>
</html>
curl -i "http://localhost:3000/never" 0.01s user 0.01s system 0% cpu 2.033 total
var express = require("express");
var connectTimeout = require("connect-timeout");
var app = express();
app.use(connectTimeout(2000));
app.get("/never", function(req, res) {
res.status(200);
// never call `res.send("foo")` or `res.json({ bar: "baz" })`
});
app.listen(3000);
$ npm i -P express connect-timeout
+ connect-timeout@1.9.0
+ express@4.16.4
updated 2 packages in 0.885s
$ node server.js
ServiceUnavailableError: Response timeout
at IncomingMessage.<anonymous> (/Users/josh/code/ecmascript/express_timeout/node_modules/connect-timeout/index.js:84:8)
at emitOne (events.js:116:13)
at IncomingMessage.emit (events.js:211:7)
at Timeout._onTimeout (/Users/josh/code/ecmascript/express_timeout/node_modules/connect-timeout/index.js:49:11)
at ontimeout (timers.js:482:11)
at tryOnTimeout (timers.js:317:5)
at Timer.listOnTimeout (timers.js:277:5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment