UNPKG

3.31 kBJavaScriptView Raw
1
2var util = require("../lib/util")
3, log = require("../log")("server")
4
5
6module.exports = listen
7
8
9function listen() {
10 var exiting
11 , server = this
12 , options = server.options
13
14 process.on("uncaughtException", function(e) {
15 ;(options.errorLog || log.error)(
16 "\nUNCAUGHT EXCEPTION!\n" +
17 (e.stack || (e.name || "Error") + ": " + (e.message || e))
18 )
19 ;(options.exit || exit).call(server, 1)
20 })
21
22 process.on("SIGINT", function() {
23 if (exiting) {
24 log.info("Killing from SIGINT (got Ctrl-C twice)")
25 return process.exit()
26 }
27 exiting = true
28 log.info("Gracefully shutting down from SIGINT (Ctrl-C)")
29 ;(options.exit || exit).call(server, 0)
30 })
31
32 process.on("SIGTERM", function() {
33 log.info("Gracefully shutting down from SIGTERM (kill)")
34 ;(options.exit || exit).call(server, 0)
35 })
36
37 process.on("SIGHUP", function() {
38 log.info("Reloading configuration from SIGHUP")
39 server.listen(true)
40 })
41
42 server.listen = options.listen || _listen
43
44 server.listen()
45
46 return server
47}
48
49function _listen() {
50 var server = this
51 , options = server.options
52 , httpsOptions = options.https || options.http2
53
54 if (server.httpServer) server.httpServer.close()
55 if (server.httpsServer) server.httpsServer.close()
56 server.httpServer = server.httpsServer = null
57
58 if (options.httpPort) {
59 server.httpServer = require("http")
60 .createServer(options.forceHttps ? forceHttps : this)
61 .listen(
62 options.httpPort,
63 options.httpHost || "0.0.0.0",
64 onListen("http")
65 )
66 }
67
68 if (httpsOptions && httpsOptions.port) {
69 server.httpsServer = (
70 options.http2 ?
71 require("http2").createSecureServer(httpsOptions, this) :
72 require("https").createServer(httpsOptions, this)
73 )
74 .listen(
75 httpsOptions.port,
76 options.httpsHost || options.httpHost || "0.0.0.0",
77 onListen(options.http2 ? "http2" : "https")
78 )
79
80 if (httpsOptions.sessionReuse) {
81 var sessionStore = {}
82 , timeout = httpsOptions.sessionTimeout || 300
83
84 server.httpsServer
85 .on("newSession", function(id, data, cb) {
86 sessionStore[id] = data
87 cb()
88 })
89 .on("resumeSession", function(id, cb) {
90 cb(null, sessionStore[id] || null)
91 })
92 }
93 }
94
95 function onListen(proto) {
96 return function() {
97 var addr = this.address()
98 log.info("Listening %s at %s:%s", proto, addr.address, addr.port)
99 this
100 .on("close", function() {
101 log.info("Stop listening %s at %s:%s", proto, addr.address, addr.port)
102 })
103 .on("connection", setNoDelay)
104 }
105 }
106
107 function setNoDelay(socket) {
108 socket.setNoDelay(true)
109 }
110
111 function forceHttps(req, res) {
112 var port = httpsOptions && httpsOptions.port || 8443
113 , host = (req.headers.host || "localhost").split(":")[0]
114 , url = "https://" + (port == 443 ? host : host + ":" + port) + req.url
115
116 res.writeHead(301, {"Content-Type": "text/html", "Location": url})
117 res.end('Redirecting to <a href="' + url + '">' + url + '</a>')
118 }
119}
120
121function exit(code) {
122 var server = this
123 , softKill = util.wait(function() {
124 log.info("Everything closed cleanly")
125 process.exit(code)
126 }, 1)
127
128 server.emit("beforeExit", softKill)
129
130 try {
131 if (server.httpServer) server.httpServer.close().unref()
132 if (server.httpsServer) server.httpsServer.close().unref()
133 } catch(e) {}
134
135 setTimeout(function() {
136 log.warn("Kill (timeout)")
137 process.exit(code)
138 }, 5000).unref()
139
140 softKill()
141}
142
143