From 714e876777c2804078d35077ac9be6f861a524c3 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Wed, 16 Dec 2015 12:37:53 +1300 Subject: [PATCH] Give user chance to specify their own default http request handler --- server.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/server.js b/server.js index ce2a453..f61dc3e 100644 --- a/server.js +++ b/server.js @@ -89,12 +89,17 @@ function Server (opts) { if (opts.ws !== false) { if (!self.http) { self.http = http.createServer() - self.http.on('request', function (req, res) { - res.statusCode = 404 - res.end('404 Not Found') - }) self.http.on('error', function (err) { self._onError(err) }) self.http.on('listening', onListening) + + // Add default http request handler if user does not add one on same tick + process.nextTick(function () { + if (self.http.listenerCount('request') > 0) return + self.http.on('request', function (req, res) { + res.statusCode = 404 + res.end('404 Not Found') + }) + }) } self.ws = new WebSocketServer({ server: self.http }) self.ws.address = self.http.address.bind(self.http)