Skip to content

Commit 67c4ab9

Browse files
committed
refactor(server): added moved express config logic into its own file
trying to make the main server.js file more lightweight. also fixed index when ngroute is disabled
1 parent 598c69a commit 67c4ab9

File tree

6 files changed

+58
-34
lines changed

6 files changed

+58
-34
lines changed

Diff for: app/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ Generator.prototype.appJs = function appJs() {
368368
};
369369

370370
Generator.prototype.createIndex = function createIndex() {
371+
this.indexFile = this.indexFile.replace(/'/g, "'");
371372
if (this.jade) {
372373
this.write(path.join(this.appPath, 'views', 'index.jade'), this.indexFile);
373374
} else {
@@ -437,6 +438,8 @@ Generator.prototype.serverFiles = function () {
437438
this.template('../../templates/express/server.js', 'server.js');
438439
this.template('../../templates/express/api.js', 'lib/controllers/api.js');
439440
this.template('../../templates/express/index.js', 'lib/controllers/index.js');
441+
this.template('../../templates/express/config/express.js', 'lib/config/express.js');
442+
this.template('../../templates/express/config/middlewares/nocache.js', 'lib/config/middlewares/nocache.js');
440443
};
441444

442445
Generator.prototype.mongoFiles = function () {

Diff for: templates/express/config/express.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
var express = require('express'),
4+
path = require('path');
5+
6+
module.exports = function(app) {
7+
var rootPath = path.normalize(__dirname + '/../..');
8+
9+
app.configure('development', function(){
10+
app.use(require('connect-livereload')());
11+
app.use(express.static(path.join(rootPath, '.tmp')));
12+
app.use(express.static(path.join(rootPath, 'app')));
13+
app.use(express.errorHandler());
14+
app.set('views', rootPath + '/app/views');
15+
});
16+
17+
app.configure('production', function(){
18+
app.use(express.favicon(path.join(rootPath, 'public', 'favicon.ico')));
19+
app.use(express.static(path.join(rootPath, 'public')));
20+
app.set('views', rootPath + '/views');
21+
});
22+
23+
app.configure(function(){<% if (!jade) { %>
24+
app.engine('html', require('ejs').renderFile);
25+
app.set('view engine', 'html');<% } %><% if (jade) { %>
26+
app.set('view engine', 'jade');<% } %>
27+
app.use(express.logger('dev'));
28+
app.use(express.bodyParser());
29+
app.use(express.methodOverride());
30+
31+
// Router needs to be last
32+
app.use(app.router);
33+
});
34+
};

Diff for: templates/express/config/middlewares/nocache.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict';
2+
3+
module.exports = function(req, res, next) {
4+
res.header('Cache-Control', 'no-cache, private, no-store, must-revalidate');
5+
return next();
6+
};

Diff for: templates/express/server.js

+13-31
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22

33
// Module dependencies.
4-
var express = require('express'),
5-
path = require('path')<% if (mongo) { %>,
4+
var express = require('express')<% if (mongo) { %>,
5+
path = require('path'),
66
fs = require('fs')<% } %>;
77

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

2323
// Express Configuration
24-
app.configure('development', function(){
25-
app.use(require('connect-livereload')());
26-
app.use(express.static(path.join(__dirname, '.tmp')));
27-
app.use(express.static(path.join(__dirname, 'app')));
28-
app.use(express.errorHandler());
29-
app.set('views', __dirname + '/app/views');
30-
});
31-
32-
app.configure('production', function(){
33-
app.use(express.favicon(path.join(__dirname, 'public', 'favicon.ico')));
34-
app.use(express.static(path.join(__dirname, 'public')));
35-
app.set('views', __dirname + '/views');
36-
});
37-
38-
app.configure(function(){<% if (!jade) { %>
39-
app.engine('html', require('ejs').renderFile);
40-
app.set('view engine', 'html');<% } %><% if (jade) { %>
41-
app.set('view engine', 'jade');<% } %>
42-
app.use(express.logger('dev'));
43-
app.use(express.bodyParser());
44-
app.use(express.methodOverride());
45-
46-
// Router needs to be last
47-
app.use(app.router);
48-
});
24+
require('./lib/config/express')(app);
4925

5026
// Controllers
5127
var api = require('./lib/controllers/api'),
52-
controllers = require('./lib/controllers');
28+
index = require('./lib/controllers');
29+
30+
// Middlewares
31+
var noCache = require('./lib/config/middlewares/nocache');
5332

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

5736
// Angular Routes
58-
app.get('/partials/*', controllers.partials);
59-
app.get('/*', controllers.index);
37+
app.get('/partials/*', noCache, index.partials);
38+
app.get('/*', index.index);
6039

6140
// Start server
6241
var port = process.env.PORT || 3000;
6342
app.listen(port, function () {
6443
console.log('Express server listening on port %d in %s mode', port, app.get('env'));
65-
});
44+
});
45+
46+
// Expose app
47+
exports = module.exports = app;

Diff for: templates/views/html/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<% if (ngRoute) {
2929
%><div class="container" ng-view></div><%
3030
} else {
31-
%><div class="container" ng-include="'views/main'" ng-controller="MainCtrl"></div><%
31+
%><div class="container" ng-include="'partials/main'" ng-controller="MainCtrl"></div><%
3232
} %>
3333

3434
<!-- Google Analytics: change UA-XXXXX-X to be your site's ID -->

Diff for: templates/views/jade/index.jade

+1-2
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@ html.no-js
3030
<![endif]
3131
3232
// Add your site or application content here
33-
div(class="container" ng-view)
3433
<% if (ngRoute) { %>
3534
div(class="container" ng-view)<%
3635
} else { %>
37-
div(class="container" ng-include="'views/main'" ng-controller="MainCtrl")<% } %>
36+
div(class="container" ng-include="'partials/main'" ng-controller="MainCtrl")<% } %>
3837

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

0 commit comments

Comments
 (0)