Permalink
Browse files

Support ExpressJS 3

  • Loading branch information...
1 parent 9a89e85 commit 43d70900031227a9762f2aa8061e104a409a2948 @biggora committed Dec 20, 2012
View
@@ -2,45 +2,28 @@
* Module dependencies.
*/
var cluster = require('cluster');
+var numCPUs = require('os').cpus().length;
var app;
/**
* Initial bootstrapping
*/
exports.boot = function(port,path){
-
- //Create our express instance
- app = require('./app').boot();
-
- // TODO : ENABLE Reload
- /**
- * var watchFolders = [path + '/models',
- path + '/controllers',
- path + '/views',
- path + '/utils']
-
- .use(cluster.reload(watchFolders,{ signal: 'SIGQUIT', interval: 60000 }))
-
- */
-
- cluster(app)
- .set('working directory', path)
- .set('socket path',path)
- .in('development')
- .set('workers', 1)
- .use(cluster.logger(path + '/logs', 'debug'))
- .use(cluster.debug())
- .use(cluster.pidfiles(path + '/pids'))
- .in('test')
- .set('workers', 1)
- .use(cluster.logger(path + '/logs', 'warning'))
- .use(cluster.pidfiles(path + '/pids'))
- .in('production')
- .set('workers', 2)
- .use(cluster.logger(path + '/logs'))
- .use(cluster.pidfiles(path + '/pids'))
- .in('all')
- .listen(parseInt(port));
-
-};
+ //Create our express instance
+ app = require('./app').boot();
+
+ if (cluster.isMaster) {
+ // Fork workers.
+ for (var i = 0; i < numCPUs; i++) {
+ cluster.fork();
+ }
+
+ cluster.on('death', function(worker) {
+ console.log('worker ' + worker.pid + ' died');
+ });
+ } else {
+ console.log("worker: %s", process.env.NODE_WORKER_ID);
+ app.listen(parseInt(port));
+ }
+};
View
@@ -2,23 +2,24 @@
* Module dependencies.
*/
var fs = require('fs'),
-express = require('express'),
-mongoose = require('mongoose'),
-nodepath = require('path');
+ express = require('express'),
+ mongoose = require('mongoose'),
+ nodepath = require('path'),
+ engine = require('ejs-locals');
-var path = __dirname;
+var curpath = __dirname.replace(/\\/gi,"/");
var app;
/**
* Initial bootstrapping
*/
-exports.boot = function(params){
+exports.boot = function (params) {
//Create our express instance
- app = express.createServer();
+ app = express();
// Import configuration
- require(path + '/conf/configuration.js')(app,express);
+ require(curpath + '/conf/configuration.js')(app, express);
// Bootstrap application
bootApplication(app);
@@ -37,62 +38,73 @@ exports.boot = function(params){
function bootApplication(app) {
- // launch
- // app.use(express.logger({ format: ':method :url :status' }));
- app.use(express.bodyParser());
- app.use(express.methodOverride());
- app.use(express.cookieParser());
- app.use(express.session({
- secret: 'helloworld'
- }));
- app.use(express.static(path + '/public')); // Before router to enable dynamic routing
- app.use(app.router);
-
- // Example 500 page
- app['error'](function(err, req, res){
- console.log('Internal Server Error: ' + err.message);
- res.render('500');
- });
-
- // Example 404 page via simple Connect middleware
- app.use(function(req, res){
- res.render('404');
- });
-
- // Setup ejs views as default, with .html as the extension
- app.set('views', path + '/views');
- app.register('.html', require('ejs'));
- app.set('view engine', 'html');
-
- // Some dynamic view helpers
- app.dynamicHelpers({
+ app.configure(function () {
+ app.use(express.logger({ format: ':method :url :status' }));
+ app.use(express.compress());
+ app.use(express.bodyParser({
+ uploadDir: curpath + '/uploads',
+ keepExtensions: true,
+ encoding: 'utf-8'
+ }));
+ app.use(express.methodOverride());
+ app.use(express.cookieParser('77ecf30e77123e7ddf1db738eaa437376'));
+ app.use(express.session({
+ cookie: {
+ maxAge: 8640000
+ },
+ secret: '77ecf30e77123e7ddf1db738eaa437376'
+ }));
+ app.use(express.static(curpath + '/public')); // Before router to enable dynamic routing
+ app.use(app.router);
+
+ // Example 500 page
+ app.use(function(err, req, res){
+ console.log('Internal Server Error: ' + err.message);
+ res.render('500');
+ });
+
+ // Example 404 page via simple Connect middleware
+ app.use(function (req, res) {
+ res.render('404');
+ });
- request: function(req){
- return req;
- },
+ // Setup ejs views as default, with .html as the extension
+
+ app.set('port', process.env.PORT || 3000);
+ app.set('views', curpath + '/views');
+ app.engine('html', engine);
+ app.set('view engine', 'html');
+ app.set('view options', {
+ complexNames: true
+ });
- hasMessages: function(req){
- return Object.keys(req.session.flash || {}).length;
- },
+ // Some dynamic view helpers
+ app.use(function (){
+ return function (req, res, next) {
- messages: function(req){
- return function(){
+ function messages() {
var msgs = req.flash();
- console.log(msgs);
- return Object.keys(msgs).reduce(function(arr, type){
+ return Object.keys(msgs).reduce(function (arr, type) {
return arr.concat(msgs[type]);
}, []);
}
- }
+
+ app.locals({
+ request: req,
+ hasMessages: Object.keys(req.session.flash || {}).length,
+ messages: messages()
+ });
+ next();
+ }});
});
}
//Bootstrap models
function bootModels(app) {
- fs.readdir(path + '/models', function(err, files){
+ fs.readdir(curpath + '/models', function (err, files) {
if (err) throw err;
- files.forEach(function(file){
+ files.forEach(function (file) {
bootModel(app, file);
});
});
@@ -104,33 +116,31 @@ function bootModels(app) {
// Bootstrap controllers
function bootControllers(app) {
- fs.readdir(path + '/controllers', function(err, files){
+ fs.readdir(curpath + '/controllers', function (err, files) {
if (err) throw err;
- files.forEach(function(file){
+ files.forEach(function (file) {
// bootController(app, file);
- });
-
-
+ });
});
- require(path + '/controllers/AppController')(app); // Include
+ require(curpath + '/controllers/AppController')(app); // Include
}
// simplistic model support
function bootModel(app, file) {
var name = file.replace('.js', ''),
- schema = require(path + '/models/'+ name); // Include the mongoose file
+ schema = require(curpath + '/models/' + name); // Include the mongoose file
}
// Load the controller, link to its view file from here
function bootController(app, file) {
var name = file.replace('.js', ''),
- controller = path + '/controllers/' + name, // full controller to include
- template = name.replace('Controller','').toLowerCase(); // template folder for html - remove the ...Controller part.
+ controller = curpath + '/controllers/' + name, // full controller to include
+ template = name.replace('Controller', '').toLowerCase(); // template folder for html - remove the ...Controller part.
// Include the controller
// require(controller)(app,template); // Include
@@ -141,5 +151,4 @@ function bootController(app, file) {
if (!module.parent) {
exports.boot().listen(3000);
console.log("Express server %s listening on port %d", express.version, app.address().port)
-}
-
+}
View
@@ -0,0 +1,12 @@
+{
+ "name": "hello-world",
+ "description": "hello world test app",
+ "version": "0.0.1",
+ "private": true,
+ "dependencies": {
+ "express" : "3.x",
+ "ejs-locals" : ">=1.0.2",
+ "socket.io" : ">=0.8.0",
+ "mongoose" : ">=3.0.0"
+ }
+}
Oops, something went wrong.

0 comments on commit 43d7090

Please sign in to comment.