Skip to content

Commit

Permalink
fix port restart issue #148
Browse files Browse the repository at this point in the history
  • Loading branch information
joelanman committed Feb 18, 2016
1 parent c1e0878 commit b391c63
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ router.get('/', function (req, res) {

router.get('/examples/template-data', function (req, res) {

res.render('examples/template-data', { 'name' : 'Foo' });
res.render('examples/template-data', { 'name' : 'Food' });

});

Expand Down
27 changes: 21 additions & 6 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var basicAuth = require('basic-auth'),
prompt = require('prompt'),
portScanner = require('portscanner');
portScanner = require('portscanner'),
fs = require('fs');

/**
* Simple basic auth middleware for use with Express 4.x.
Expand Down Expand Up @@ -35,17 +36,26 @@ exports.basicAuth = function(username, password) {

exports.findAvailablePort = function(app){

var port = (process.env.PORT || 3000);
var port = null;

try {
port = Number(fs.readFileSync(__dirname+'/../port.tmp'));
} catch (e){
port = (process.env.PORT || 3000);
}

console.log('');

// Check that default port is free, else offer to change
portScanner.findAPortNotInUse(port, port+50, '127.0.0.1', function(error, availablePort) {

if (port == availablePort){

app.listen(port);
console.log('Listening on port ' + port + ' url: http://localhost:' + port);
}
else {

} else {

// Default port in use - offer to change to available port
console.error("ERROR: Port " + port + " in use - you may have another prototype running.\n");
// Set up prompt settings
Expand All @@ -63,17 +73,22 @@ exports.findAvailablePort = function(app){
pattern: /y(es)?|no?/i,
message: 'Please enter y or n'
}], function (err, result) {

if (result.answer.match(/y(es)?/i) ) {

// User answers yes
port = availablePort;
fs.writeFileSync(__dirname+'/../port.tmp', port);
app.listen(port);
console.log('Changed to port ' + port + ' url: http://localhost:' + port);
}
else {

} else {

// User answers no - exit
console.log('\nYou can set a new default port in server.js, or by running the server with PORT=XXXX');
console.log("\nExit by pressing 'ctrl + c'");
process.exit(0);

}
});
}
Expand Down
13 changes: 11 additions & 2 deletions start.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
// Check for `node_modules` folder and warn if missing
var fs = require('fs');

if (!fs.existsSync(__dirname + '/node_modules')) {
console.error('ERROR: Node module folder missing. Try running `npm install`');
process.exit(0);
}

var gruntfile = __dirname + '/Gruntfile.js';

require(__dirname + '/node_modules/grunt/lib/grunt.js').cli({
'gruntfile' : gruntfile
});

process.on('SIGINT', function() {
process.kill(process.pid, 'SIGTERM');
process.exit();

// remove port.tmp if it exists
try {
fs.unlinkSync(__dirname + '/port.tmp');
} catch(e){}

// process.kill(process.pid, 'SIGTERM');
process.exit(0);

});

0 comments on commit b391c63

Please sign in to comment.