Permalink
Browse files

Fixes #17 - Upgrade for Express 4.x

  • Loading branch information...
1 parent 4ab32db commit 57fd0465c87c087de4175d9883e52226b093864c @jrcryer committed Apr 29, 2014
Showing with 87 additions and 77 deletions.
  1. +8 −0 app/templates/_package.json
  2. +7 −9 app/templates/_server.js
  3. +14 −4 app/templates/config/config.js
  4. +58 −64 app/templates/config/express.js
@@ -18,6 +18,14 @@
},
"dependencies": {
"express": "latest",
+ "morgan": "latest",
+ "cookie-parser": "latest",
+ "serve-static": "latest",
+ "body-parser": "latest",
+ "compression": "latest",
+ "method-override": "latest",
+ "express-session": "latest",
+ "static-favicon": "latest",
"consolidate": "latest",
"jade": "latest",
"mongoose": "latest",
View
@@ -1,22 +1,20 @@
+'use strict';
+
/**
* Module dependencies.
*/
var express = require('express'),
- fs = require('fs');
+ fs = require('fs'),
+ mongoose = require('mongoose');
/**
* Main application entry file.
* Please note that the order of loading is important.
*/
-//Load configurations
-//if test env, load example file
-var env = process.env.NODE_ENV = process.env.NODE_ENV || 'development',
- config = require('./config/config'),
- mongoose = require('mongoose');
-
-//Bootstrap db connection
-var db = mongoose.connect(config.db);
+// Initializing system variables
+var config = require('./config/config');
+var db = mongoose.connect(config.db);
//Bootstrap models
var models_path = __dirname + '/app/models';
@@ -1,7 +1,17 @@
-var _ = require('underscore');
+'use strict';
-// Load app configuration
+var _ = require('underscore'),
+ fs = require('fs');
+// Load configurations
+// Set the node environment variable if not set before
+process.env.NODE_ENV = ~fs.readdirSync('./config/env').map(function(file) {
+ return file.slice(0, -3);
+}).indexOf(process.env.NODE_ENV) ? process.env.NODE_ENV : 'development';
+
+// Extend the base configuration in all.js with environment
+// specific configuration
module.exports = _.extend(
- require(__dirname + '/../config/env/all.js'),
- require(__dirname + '/../config/env/' + process.env.NODE_ENV + '.json') || {});
+ require('./env/all'),
+ require('./env/' + process.env.NODE_ENV) || {}
+);
@@ -1,12 +1,20 @@
/**
* Module dependencies.
*/
-var express = require('express'),
- consolidate = require('consolidate'),
- mongoStore = require('connect-mongo')(express),
- flash = require('connect-flash'),
- helpers = require('view-helpers'),
- config = require('./config');
+var express = require('express'),
+ consolidate = require('consolidate'),
+ session = require('express-session'),
+ helpers = require('view-helpers'),
+ morgan = require('morgan'),
+ cookieParser = require('cookie-parser'),
+ serveStatic = require('serve-static'),
+ bodyParser = require('body-parser'),
+ compression = require('compression'),
+ methodOverride = require('method-override'),
+ favicon = require('static-favicon'),
+ MongoStore = require('connect-mongo')({session: session}),
+ flash = require('connect-flash'),
+ config = require('./config');
module.exports = function(app, db) {
app.set('showStackError', true);
@@ -15,10 +23,10 @@ module.exports = function(app, db) {
app.locals.pretty = true;
// cache=memory or swig dies in NODE_ENV=production
- app.locals.cache = 'memory';
+ app.locals.cache = 'memory';
- //Should be placed before express.static
- app.use(express.compress({
+ //Should be placed before express
+ app.use(compression({
filter: function(req, res) {
return (/json|text|javascript|css/).test(res.getHeader('Content-Type'));
},
@@ -27,7 +35,7 @@ module.exports = function(app, db) {
//Don't use logger for test env
if (process.env.NODE_ENV === 'development') {
- app.use(express.logger('dev'));
+ app.use(morgan('dev'));
}
// assign the template engine to .html files
@@ -42,61 +50,47 @@ module.exports = function(app, db) {
// Enable jsonp
app.enable('jsonp callback');
- app.configure(function() {
- // The cookieParser should be above session
- app.use(express.cookieParser());
-
- // Request body parsing middleware should be above methodOverride
- app.use(express.urlencoded());
- app.use(express.json());
- app.use(express.methodOverride());
-
- // Express/Mongo session storage
- app.use(express.session({
- secret: config.sessionSecret,
- store: new mongoStore({
- db: db.connection.db,
- collection: config.sessionCollection
- })
- }));
-
- // Dynamic helpers
- app.use(helpers(config.app.name));
-
- // Connect flash for flash messages
- app.use(flash());
-
- // Routes should be at the last
- app.use(app.router);
-
- // Setting the fav icon and static folder
- app.use(express.favicon());
- app.use(express.static(config.root + '/public'));
- app.use('/lib', express.static(config.root + '/app/components'));
-
- // Assume "not found" in the error msgs is a 404. this is somewhat
- // silly, but valid, you can do whatever you like, set properties,
- // use instanceof etc.
- app.use(function(err, req, res, next) {
- // Treat as 404
- if (~err.message.indexOf('not found')) return next();
-
- // Log it
- console.error(err.stack);
-
- // Error page
- res.status(500).render('500', {
- error: err.stack
- });
- });
+ // The cookieParser should be above session
+ app.use(cookieParser());
+
+ // Request body parsing middleware should be above methodOverride
+ app.use(bodyParser.urlencoded());
+ app.use(bodyParser.json());
+ app.use(methodOverride());
+
+ // Express/Mongo session storage
+ app.use(session({
+ secret: config.sessionSecret,
+ store: new MongoStore({
+ db: db.connection.db,
+ collection: config.sessionCollection
+ })
+ }));
- // Assume 404 since no middleware responded
- app.use(function(req, res) {
- res.status(404).render('404', {
- url: req.originalUrl,
- error: 'Not found'
- });
- });
+ // Dynamic helpers
+ app.use(helpers(config.app.name));
+
+ // Connect flash for flash messages
+ app.use(flash());
+ // Setting the fav icon and static folder
+ app.use(favicon(config.root + '/public/favicon.ico'));
+ app.use(serveStatic(config.root + '/public'));
+ app.use('/lib', serveStatic(config.root + '/app/components'));
+
+ // Assume "not found" in the error msgs is a 404. this is somewhat
+ // silly, but valid, you can do whatever you like, set properties,
+ // use instanceof etc.
+ app.use(function(err, req, res, next) {
+ // Treat as 404
+ if (~err.message.indexOf('not found')) return next();
+
+ // Log it
+ console.error(err.stack);
+
+ // Error page
+ res.status(500).render('500', {
+ error: err.stack
+ });
});
};

0 comments on commit 57fd046

Please sign in to comment.