Increment port feature

If the specified port is being used, added a feature that it would
increment the port by 1 and try again, logging the chosen port to the
console.

removed console.log of config
This commit is contained in:
billchurch 2016-05-20 10:50:46 -04:00
parent 9a9cf5cdc0
commit bc20a06497

View file

@ -11,6 +11,20 @@ var readConfig = require('read-config'),
console.log(config);
var isPortTaken = function(port, fn) {
var net = require('net')
var tester = net.createServer()
.once('error', function (err) {
if (err.code != 'EADDRINUSE') return fn(err)
fn(null, true)
})
.once('listening', function() {
tester.once('close', function() { fn(null, false) })
.close()
})
.listen(port)
}
function checkParams(arr) {
return function(req, res, next) {
// Make sure each param listed in arr is present in req.query
@ -34,6 +48,14 @@ function checkParams(arr) {
server.listen({
host: config.listen.ip,
port: config.listen.port
}).on('error', function (err) {
if (err.code === 'EADDRINUSE') {
config.listen.port++;
console.log('Address in use, retrying on port ' + config.listen.port);
setTimeout(function () {
server.listen(config.listen.port);
}, 250);
}
});
app.use(express.static(__dirname + '/public')).use(term.middleware()).use(function(req, res, next) {