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", diff --git a/server.js b/server.js index b8bd2c3c..cb67cd3e 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,26 @@ app.use(webpackHotMiddleware(compiler, { heartbeat: 10 * 1000 })); -app.listen(PORT, 'localhost', err => { - if (err) { - console.error(err); +// 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) => { + + if (ports.length === 0) { + console.error(`This is rather embarrasing : none of the given ports are available`); return; } - console.log(`Listening at http://localhost:${PORT}`); + // 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}`); + }); + }); + +