Skip to content

Commit

Permalink
refactor(server): added moved express config logic into its own file
Browse files Browse the repository at this point in the history
trying to make the main server.js file more lightweight.
also fixed index when ngroute is disabled
  • Loading branch information
DaftMonk committed Dec 22, 2013
1 parent 598c69a commit 67c4ab9
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 34 deletions.
3 changes: 3 additions & 0 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ Generator.prototype.appJs = function appJs() {
};

Generator.prototype.createIndex = function createIndex() {
this.indexFile = this.indexFile.replace(/'/g, "'");
if (this.jade) {
this.write(path.join(this.appPath, 'views', 'index.jade'), this.indexFile);
} else {
Expand Down Expand Up @@ -437,6 +438,8 @@ Generator.prototype.serverFiles = function () {
this.template('../../templates/express/server.js', 'server.js');
this.template('../../templates/express/api.js', 'lib/controllers/api.js');
this.template('../../templates/express/index.js', 'lib/controllers/index.js');
this.template('../../templates/express/config/express.js', 'lib/config/express.js');
this.template('../../templates/express/config/middlewares/nocache.js', 'lib/config/middlewares/nocache.js');
};

Generator.prototype.mongoFiles = function () {
Expand Down
34 changes: 34 additions & 0 deletions templates/express/config/express.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

var express = require('express'),
path = require('path');

module.exports = function(app) {
var rootPath = path.normalize(__dirname + '/../..');

app.configure('development', function(){
app.use(require('connect-livereload')());
app.use(express.static(path.join(rootPath, '.tmp')));
app.use(express.static(path.join(rootPath, 'app')));
app.use(express.errorHandler());
app.set('views', rootPath + '/app/views');
});

app.configure('production', function(){
app.use(express.favicon(path.join(rootPath, 'public', 'favicon.ico')));
app.use(express.static(path.join(rootPath, 'public')));
app.set('views', rootPath + '/views');
});

app.configure(function(){<% if (!jade) { %>
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');<% } %><% if (jade) { %>
app.set('view engine', 'jade');<% } %>
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());

// Router needs to be last
app.use(app.router);
});
};
6 changes: 6 additions & 0 deletions templates/express/config/middlewares/nocache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports = function(req, res, next) {
res.header('Cache-Control', 'no-cache, private, no-store, must-revalidate');
return next();
};
44 changes: 13 additions & 31 deletions templates/express/server.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

// Module dependencies.
var express = require('express'),
path = require('path')<% if (mongo) { %>,
var express = require('express')<% if (mongo) { %>,
path = require('path'),
fs = require('fs')<% } %>;

var app = express();
Expand All @@ -21,45 +21,27 @@ require('./lib/db/dummydata');
<% } %>

// Express Configuration
app.configure('development', function(){
app.use(require('connect-livereload')());
app.use(express.static(path.join(__dirname, '.tmp')));
app.use(express.static(path.join(__dirname, 'app')));
app.use(express.errorHandler());
app.set('views', __dirname + '/app/views');
});

app.configure('production', function(){
app.use(express.favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(express.static(path.join(__dirname, 'public')));
app.set('views', __dirname + '/views');
});

app.configure(function(){<% if (!jade) { %>
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');<% } %><% if (jade) { %>
app.set('view engine', 'jade');<% } %>
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());

// Router needs to be last
app.use(app.router);
});
require('./lib/config/express')(app);

// Controllers
var api = require('./lib/controllers/api'),
controllers = require('./lib/controllers');
index = require('./lib/controllers');

// Middlewares
var noCache = require('./lib/config/middlewares/nocache');

// Server Routes
app.get('/api/awesomeThings', api.awesomeThings);

// Angular Routes
app.get('/partials/*', controllers.partials);
app.get('/*', controllers.index);
app.get('/partials/*', noCache, index.partials);
app.get('/*', index.index);

// Start server
var port = process.env.PORT || 3000;
app.listen(port, function () {
console.log('Express server listening on port %d in %s mode', port, app.get('env'));
});
});

// Expose app
exports = module.exports = app;
2 changes: 1 addition & 1 deletion templates/views/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<% if (ngRoute) {
%><div class="container" ng-view></div><%
} else {
%><div class="container" ng-include="'views/main'" ng-controller="MainCtrl"></div><%
%><div class="container" ng-include="'partials/main'" ng-controller="MainCtrl"></div><%
} %>

<!-- Google Analytics: change UA-XXXXX-X to be your site's ID -->
Expand Down
3 changes: 1 addition & 2 deletions templates/views/jade/index.jade
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ html.no-js
<![endif]
// Add your site or application content here
div(class="container" ng-view)
<% if (ngRoute) { %>
div(class="container" ng-view)<%
} else { %>
div(class="container" ng-include="'views/main'" ng-controller="MainCtrl")<% } %>
div(class="container" ng-include="'partials/main'" ng-controller="MainCtrl")<% } %>

// Google Analytics: change UA-XXXXX-X to be your site's ID.
script.
Expand Down

0 comments on commit 67c4ab9

Please sign in to comment.