Skip to content

Commit

Permalink
Add grunt, cluster support and documentatipon for node inspector
Browse files Browse the repository at this point in the history
  • Loading branch information
Temmermans committed Aug 25, 2016
1 parent 5591591 commit f904d9d
Show file tree
Hide file tree
Showing 14 changed files with 315 additions and 33 deletions.
72 changes: 72 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
module.exports = function(grunt){
// load plugins
[
'grunt-contrib-less',
'grunt-mocha-test',
'grunt-contrib-uglify',
'grunt-contrib-cssmin',
'grunt-hashres'
].forEach(function(task){
grunt.loadNpmTasks(task);
});

// configure plugins
grunt.initConfig({
// Configure a mochaTest task
mochaTest: {
test: {
options: {
reporter: 'spec',
captureFile: 'results.txt', // Optionally capture the reporter output to a file
quiet: false, // Optionally suppress output to standard out (defaults to false)
clearRequireCache: false // Optionally clear the require cache before running tests (defaults to false)
},
src: ['test/**/*.js']
}
},
less: {
development: {
files: {
'public/styles/css/styles.css': 'public/styles/less/styles.less',
}
}
},
uglify: {
all: {
files: {
'public/js/bundle.min.js': ['public/js/**/*.js']
}
}
},
cssmin: {
combine: {
files: {
'public/styles/css/bundle.css': ['public/styles/css/**/*.css',
'!public/styles/css/bundle*.css']
}
},
minify: {
src: 'public/styles/css/bundle.css',
dest: 'public/css/bundle.min.css',
}
},
hashres: {
options: {
fileNameFormat: '${name}.${hash}.${ext}'
},
all: {
src: [
'public/js/bundle.min.js',
'public/css/bundle.min.css',
],
dest: [
'views/layouts/layout.pug',
]
},
}
});

// register tasks
grunt.registerTask('test', ['mochaTest']);
grunt.registerTask('bunminify', ['uglify', 'less', 'cssmin', 'hashres']);
};
162 changes: 138 additions & 24 deletions bin/www
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
*/

var app = require('../server');
var debug = require('debug')('myapp:server');
var cluster = require('cluster');
var debug = require('debug')('temp:server');
var http = require('http');
var https = require('https');
var fs = require('fs');
var numCPUs = require('os').cpus().length;

/**
* Get port from environment and store in Express.
Expand All @@ -17,31 +17,40 @@ var fs = require('fs');
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
* Create HTTP server.
*/
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}

//read out the key and cert when creating an http server
// var sslkey = fs.readFileSync('ssl-key.pem');
// var sslcert = fs.readFileSync('ssl-cert.pem');
//
// var options = {
// key: sslkey,
// cert: sslcert
// };
// If a worker dies, log it to the console and start another worker.
cluster.on('exit', function(worker, code, signal) {
console.log('Worker ' + worker.process.pid + ' died.');
cluster.fork();
});

var server = http.createServer(app);
// var server = https.createServer(options, app);
// Log when a worker starts listening
cluster.on('listening', function(worker, address) {
console.log('Worker started with PID ' + worker.process.pid + '.');
});

/**
* Listen on provided port, on all network interfaces.
*/
} else {
/**
* Create HTTP server.
*/

var server = http.createServer(app);

server.listen(port, function() {
console.log("App listening on port: " + port);
});
server.on('error', onError);
server.on('listening', onListening);
/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
}

// The rest of the bin/www file.....

/**
* Normalize a port into a number, string, or false.
Expand Down Expand Up @@ -98,3 +107,108 @@ function onListening() {
var bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port;
debug('Listening on ' + bind);
}




////////// WITHOUT CLUSTER ////////
// #!/usr/bin/env node
//
// /**
// * Module dependencies.
// */
//
// var app = require('../server');
// var debug = require('debug')('myapp:server');
// var http = require('http');
// var https = require('https');
// var fs = require('fs');
//
// /**
// * Get port from environment and store in Express.
// */
//
// var port = normalizePort(process.env.PORT || '3000');
// app.set('port', port);
//
// /**
// * Create HTTP server.
// */
//
// //read out the key and cert when creating an http server
// // var sslkey = fs.readFileSync('ssl-key.pem');
// // var sslcert = fs.readFileSync('ssl-cert.pem');
// //
// // var options = {
// // key: sslkey,
// // cert: sslcert
// // };
//
// var server = http.createServer(app);
// // var server = https.createServer(options, app);
//
// /**
// * Listen on provided port, on all network interfaces.
// */
//
// server.listen(port, function() {
// console.log("App listening on port: " + port);
// });
// server.on('error', onError);
// server.on('listening', onListening);
//
// /**
// * Normalize a port into a number, string, or false.
// */
//
// function normalizePort(val) {
// var port = parseInt(val, 10);
//
// if (isNaN(port)) {
// // named pipe
// return val;
// }
//
// if (port >= 0) {
// // port number
// return port;
// }
//
// return false;
// }
//
// /**
// * Event listener for HTTP server "error" event.
// */
//
// function onError(error) {
// if (error.syscall !== 'listen') {
// throw error;
// }
//
// var bind = typeof port === 'string' ? 'Pipe ' + port : 'Port ' + port;
//
// // handle specific listen errors with friendly messages
// switch (error.code) {
// case 'EACCES':
// console.error(bind + ' requires elevated privileges');
// process.exit(1);
// break;
// case 'EADDRINUSE':
// console.error(bind + ' is already in use');
// process.exit(1);
// break;
// default:
// throw error;
// }
// }
//
// /**
// * Event listener for HTTP server "listening" event.
// */
//
// function onListening() {
// var addr = server.address();
// var bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port;
// debug('Listening on ' + bind);
// }
2 changes: 1 addition & 1 deletion controllers/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//the index.js file requires all other controllers plus defines some general routes that are not linked to a resource, like the home routes
//the index.js file requires all other controllers plus defines some general routes that are not linked to a resource, like the home route

var express = require('express'),
router = express.Router()
Expand Down
6 changes: 3 additions & 3 deletions controllers/resources.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//create a controller file for every model. A good convention is to pluralize it
//the controller has everything to do with routing relative to the resourece

// var express = require('express'),
// router = express.Router(),
// var express = require('express')
// var router = express.Router()
// City = require('../models/resource');
//
// router.get('/cities', function(req, res){
Expand All @@ -22,5 +22,5 @@
// }
// });
// });
//
//
// module.exports = router;
3 changes: 3 additions & 0 deletions lib/credentials.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {

};
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"main": "server.js",
"scripts": {
"test": "mocha",
"start": "forever server.js"
"start": "node server.js"
},
"author": "",
"license": "ISC",
Expand All @@ -16,7 +16,6 @@
"express": "^4.13.4",
"express-enforces-ssl": "^1.1.0",
"express-session": "^1.13.0",
"forever": "^0.15.2",
"helmet": "^2.1.0",
"morgan": "^1.7.0",
"ms": "^0.7.1",
Expand All @@ -27,6 +26,12 @@
"cheerio": "^0.20.0",
"debug": "^2.2.0",
"errorhandler": "^1.4.3",
"grunt": "^1.0.1",
"grunt-contrib-cssmin": "^1.0.1",
"grunt-contrib-less": "^1.4.0",
"grunt-contrib-uglify": "^2.0.0",
"grunt-hashres": "^0.4.1",
"grunt-mocha-test": "^0.12.7",
"mocha": "^2.5.3",
"supertest": "^1.2.0"
}
Expand Down
1 change: 1 addition & 0 deletions public/css/bundle.min.ac577c93.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
p{font-size:10px}
Empty file.
1 change: 1 addition & 0 deletions public/styles/css/bundle.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
p{font-size:10px}
4 changes: 4 additions & 0 deletions public/styles/css/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* Custom styles go here */
p {
font-size: 10px;
}
4 changes: 4 additions & 0 deletions public/styles/less/styles.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* Custom styles go here */
p {
font-size: 10px;
}
34 changes: 33 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,39 @@ $ node ./bin/www
To test the application, simply add files in the test folder (give them the same name as the resources they test) and run

```
$ npm test
$ grunt test
```

### Node Inspector

To use the node inspector follow the following steps:
```
$ npm install -g node-inspector
```

run the node inspector in a command line and open another command line
```
$ node-inspector
```

to start debugging the application run node --debug
```
$ node --debug ./bin/www
```
visit the following url: http://localhost:8080/debug?port=5858. Start debugging!

Commands:
1. Resume script execution (F8)
2. Step over next function call (F10)
3. Step into next function call (F11)
4. Step out of current function (Shift-F11)

### Grunt

Run the following command to compile LESS to CSS, minify, bundle and hash all the static resource files.

```
$ grunt bunminify
```

### ToDo
Expand Down
Loading

0 comments on commit f904d9d

Please sign in to comment.