From 71dd09d31b0ae56d848ea923456c829f97cd3d3e Mon Sep 17 00:00:00 2001 From: Erik Yuzwa Date: Sun, 18 Dec 2016 21:54:14 -0700 Subject: [PATCH 1/3] added find-port for hot-server --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 8c4011e0..f7c8625a 100644 --- a/package.json +++ b/package.json @@ -145,6 +145,7 @@ "cuid": "^1.3.8", "eventify": "^2.0.0", "express": "^4.13.4", + "find-port": "^2.0.1", "fixed-data-table-2": "^0.7.7", "highlight.js": "^8.9.1", "marked": "^0.3.5", From 17b5399f08788c59441daf184bc697e8d9801813 Mon Sep 17 00:00:00 2001 From: Erik Yuzwa Date: Sun, 18 Dec 2016 21:54:32 -0700 Subject: [PATCH 2/3] updated server startup to check port(s) --- server.js | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/server.js b/server.js index b8bd2c3c..13566473 100644 --- a/server.js +++ b/server.js @@ -3,10 +3,11 @@ import webpack from 'webpack'; import webpackDevMiddleware from 'webpack-dev-middleware'; import webpackHotMiddleware from 'webpack-hot-middleware'; import config from './webpack.dev.config.js'; +import findPort from 'find-port'; const app = express(), - compiler = webpack(config), - PORT = 3001; + compiler = webpack(config); +let PORT; app.use(webpackDevMiddleware(compiler, { publicPath: config.output.publicPath, @@ -23,11 +24,21 @@ app.use(webpackHotMiddleware(compiler, { heartbeat: 10 * 1000 })); -app.listen(PORT, 'localhost', err => { - if (err) { - console.error(err); - return; - } +// before binding our server to a port, check each port in the given range +// to ensure it's available. Upon return, just go ahead with the first element +findPort('localhost', [3001, 9000, 9001, 9002], (ports) => { + + // make use of the first available port returned + PORT = ports[0]; + app.listen(PORT, 'localhost', err => { + if (err) { + console.error(err); + return; + } + + console.log(`Listening at http://localhost:${PORT}`); + }); - console.log(`Listening at http://localhost:${PORT}`); }); + + From 566c1c8c7e5281209c93fe7014cf5219a85dd384 Mon Sep 17 00:00:00 2001 From: Erik Yuzwa Date: Sun, 18 Dec 2016 22:08:03 -0700 Subject: [PATCH 3/3] handle no available ports --- server.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/server.js b/server.js index 13566473..cb67cd3e 100644 --- a/server.js +++ b/server.js @@ -28,6 +28,11 @@ app.use(webpackHotMiddleware(compiler, { // to ensure it's available. Upon return, just go ahead with the first element findPort('localhost', [3001, 9000, 9001, 9002], (ports) => { + if (ports.length === 0) { + console.error(`This is rather embarrasing : none of the given ports are available`); + return; + } + // make use of the first available port returned PORT = ports[0]; app.listen(PORT, 'localhost', err => {