Skip to content

Commit 0a2bf2a

Browse files
committed
feat(app): restructured project for easier configuration
global configurations are now setup from the config/env folder. moved routes out of server js and into its own module
1 parent 8784106 commit 0a2bf2a

25 files changed

+158
-91
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
test/temp
33
test/UpperCaseBug
4+
.idea

Diff for: app/index.js

+14-8
Original file line numberDiff line numberDiff line change
@@ -471,9 +471,16 @@ Generator.prototype._injectDependencies = function _injectDependencies() {
471471
Generator.prototype.serverFiles = function () {
472472
this.template('../../templates/express/server.js', 'server.js');
473473
this.copy('../../templates/express/jshintrc', 'lib/.jshintrc');
474-
this.template('../../templates/express/api.js', 'lib/controllers/api.js');
475-
this.template('../../templates/express/index.js', 'lib/controllers/index.js');
474+
this.template('../../templates/express/controllers/api.js', 'lib/controllers/api.js');
475+
this.template('../../templates/express/controllers/index.js', 'lib/controllers/index.js');
476+
this.template('../../templates/express/routes.js', 'lib/routes.js');
477+
476478
this.template('../../templates/express/config/express.js', 'lib/config/express.js');
479+
this.template('../../templates/express/config/config.js', 'lib/config/config.js');
480+
this.template('../../templates/express/config/env/all.js', 'lib/config/env/all.js');
481+
this.template('../../templates/express/config/env/development.js', 'lib/config/env/development.js');
482+
this.template('../../templates/express/config/env/production.js', 'lib/config/env/production.js');
483+
this.template('../../templates/express/config/env/test.js', 'lib/config/env/test.js');
477484
};
478485

479486
Generator.prototype.mongoFiles = function () {
@@ -483,9 +490,8 @@ Generator.prototype.mongoFiles = function () {
483490
}
484491
this.env.options.mongo = this.mongo;
485492

486-
this.template('../../templates/express/mongo/mongo.js', 'lib/db/mongo.js');
487-
this.template('../../templates/express/mongo/dummydata.js', 'lib/db/dummydata.js');
488-
this.template('../../templates/express/mongo/thing.js', 'lib/models/thing.js');
493+
this.template('../../templates/express/config/dummydata.js', 'lib/config/dummydata.js');
494+
this.template('../../templates/express/models/thing.js', 'lib/models/thing.js');
489495

490496
if(!this.mongoPassportUser) {
491497
return; // Skip if disabled.
@@ -508,8 +514,8 @@ Generator.prototype.mongoFiles = function () {
508514
// config
509515
this.template('../../templates/express/config/passport.js', 'lib/config/passport.js');
510516
// models
511-
this.template('../../templates/express/mongo/user.js', 'lib/models/user.js');
517+
this.template('../../templates/express/models/user.js', 'lib/models/user.js');
512518
// controllers
513-
this.template('../../templates/express/session.js', 'lib/controllers/session.js');
514-
this.template('../../templates/express/users.js', 'lib/controllers/users.js');
519+
this.template('../../templates/express/controllers/session.js', 'lib/controllers/session.js');
520+
this.template('../../templates/express/controllers/users.js', 'lib/controllers/users.js');
515521
};

Diff for: deploy/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Generator.prototype.herokuCreate = function herokuCreate() {
6868
} else {
6969
console.log('stdout: ' + stdout);
7070
console.log(chalk.green('You\'re all set! Now push to heroku with\n\t' + chalk.bold('git push heroku master') +
71-
'\nfrom your new distribution folder'));
71+
'\nfrom your new ' + chalk.bold('dist') + ' folder'));
7272
console.log(chalk.yellow('After app modification run\n\t' + chalk.bold('grunt build') +
7373
'\nthen commit and push the dist folder'));
7474
}

Diff for: templates/common/Gruntfile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ module.exports = function (grunt) {
9292
express: {
9393
files: [
9494
'server.js',
95-
'lib/{,*//*}*.{js,json}'
95+
'lib/**/*.{js,json}'
9696
],
9797
tasks: ['newer:jshint:server', 'express:dev'],
9898
options: {

Diff for: templates/common/_package.json

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"dependencies": {
55
"express": "~3.4.3"<% if (mongo) { %>,
66
"async": "~0.2.9",
7+
"lodash": "~2.4.1",
78
"mongoose": "~3.5.5"<% } %><% if (mongo && mongoPassportUser) { %>,
89
"mongoose-unique-validator": "~0.3.0",
910
"passport": "latest",

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

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
var _ = require('lodash');
4+
5+
/**
6+
* Load environment configuration
7+
*/
8+
module.exports = _.extend(
9+
require('./env/all.js'),
10+
require('./env/' + process.env.NODE_ENV + '.js') || {});

Diff for: templates/express/mongo/dummydata.js renamed to templates/express/config/dummydata.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ var mongoose = require('mongoose'),<% if(mongo && mongoPassportUser) { %>
44
User = mongoose.model('User'),<% } %>
55
Thing = mongoose.model('Thing');
66

7+
/**
8+
* Populate database with sample application data
9+
*/
10+
711
//Clear old things, then add things in
812
Thing.find({}).remove(function() {
9-
Thing.create({
13+
Thing.create({
1014
name : 'HTML5 Boilerplate',
1115
info : 'HTML5 Boilerplate is a professional front-end template for building fast, robust, and adaptable web apps or sites.',
1216
awesomeness: 10
@@ -26,19 +30,19 @@ Thing.find({}).remove(function() {
2630
name : 'MongoDB + Mongoose',
2731
info : 'An excellent document database. Combined with Mongoose to simplify adding validation and business logic.',
2832
awesomeness: 10
29-
}, function(err) {
33+
}, function() {
3034
console.log('finished populating things');
3135
}
3236
);
3337
});
3438
<% if(mongo && mongoPassportUser) { %>
3539
// Clear old users, then add a default user
3640
User.find({}).remove(function() {
37-
User.create({
41+
User.create({
3842
name: 'Test User',
3943
4044
password: 'test'
41-
}, function(err) {
45+
}, function() {
4246
console.log('finished populating users');
4347
}
4448
);

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

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
var path = require('path');
4+
5+
var rootPath = path.normalize(__dirname + '/../../..');
6+
7+
module.exports = {
8+
root: rootPath,
9+
port: process.env.PORT || 3000<% if (mongo) { %>,
10+
mongo: {
11+
options: {
12+
db: {
13+
safe: true
14+
}
15+
}
16+
}<% } %>
17+
};

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

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
module.exports = {
4+
env: 'development'<% if (mongo) { %>,
5+
mongo: {
6+
uri: 'mongodb://localhost/fullstack-dev'
7+
}<% } %>
8+
};

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

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
module.exports = {
4+
env: 'production'<% if (mongo) { %>,
5+
mongo: {
6+
uri: process.env.MONGOLAB_URI ||
7+
process.env.MONGOHQ_URL ||
8+
'mongodb://localhost/fullstack'
9+
}<% } %>
10+
};

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

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
module.exports = {
4+
env: 'test'<% if (mongo) { %>,
5+
mongo: {
6+
uri: 'mongodb://localhost/fullstack-test'
7+
}<% } %>
8+
};

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

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

33
var express = require('express'),
4-
path = require('path')<% if (mongo && mongoPassportUser) { %>,
4+
path = require('path'),
5+
config = require('./config')<% if (mongoPassportUser) { %>,
56
passport = require('passport')<% } %>;
67

8+
/**
9+
* Express configuration
10+
*/
711
module.exports = function(app) {
8-
var rootPath = path.normalize(__dirname + '/../..');
9-
1012
app.configure('development', function(){
1113
app.use(require('connect-livereload')());
1214

@@ -20,16 +22,16 @@ module.exports = function(app) {
2022
next();
2123
});
2224

23-
app.use(express.static(path.join(rootPath, '.tmp')));
24-
app.use(express.static(path.join(rootPath, 'app')));
25+
app.use(express.static(path.join(config.root, '.tmp')));
26+
app.use(express.static(path.join(config.root, 'app')));
2527
app.use(express.errorHandler());
26-
app.set('views', rootPath + '/app/views');
28+
app.set('views', config.root + '/app/views');
2729
});
2830

2931
app.configure('production', function(){
30-
app.use(express.favicon(path.join(rootPath, 'public', 'favicon.ico')));
31-
app.use(express.static(path.join(rootPath, 'public')));
32-
app.set('views', rootPath + '/views');
32+
app.use(express.favicon(path.join(config.root, 'public', 'favicon.ico')));
33+
app.use(express.static(path.join(config.root, 'public')));
34+
app.set('views', config.root + '/views');
3335
});
3436

3537
app.configure(function(){<% if (!jade) { %>
@@ -39,10 +41,10 @@ module.exports = function(app) {
3941
app.use(express.logger('dev'));
4042
app.use(express.bodyParser());
4143
app.use(express.methodOverride());
42-
<% if(mongo && mongoPassportUser) { %>
44+
<% if(mongoPassportUser) { %>
4345
app.use(express.cookieParser());
4446
app.use(express.session({
45-
secret: 'generator-angular-fullstack-supersecret!',
47+
secret: 'angular-fullstack secret!',
4648
}));
4749

4850
//use passport session

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

-6
This file was deleted.

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

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ var mongoose = require('mongoose'),
55
passport = require('passport'),
66
LocalStrategy = require('passport-local').Strategy;
77

8+
/**
9+
* Passport configuration
10+
*/
811
module.exports = function() {
912
passport.serializeUser(function(user, done) {
1013
done(null, user.id);

Diff for: templates/express/api.js renamed to templates/express/controllers/api.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
'use strict';
22
<% if (mongo) { %>
33
var mongoose = require('mongoose'),
4-
Thing = mongoose.model('Thing'),
5-
async = require('async');
4+
Thing = mongoose.model('Thing');
65
<% } %>
76
/**
87
* Get awesome things

Diff for: templates/express/index.js renamed to templates/express/controllers/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ exports.partials = function(req, res) {
2020
/**
2121
* Send our single page app
2222
*/
23-
exports.index = function(req, res) {
23+
exports.index = function(req, res) {
2424
res.render('index');
2525
};

Diff for: templates/express/session.js renamed to templates/express/controllers/session.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

3-
var mongoose = require('mongoose'),
4-
passport = require('passport');
3+
var mongoose = require('mongoose');
54

65
/**
76
* Logout
File renamed without changes.

Diff for: templates/express/middleware.js

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

33
/**
4-
* Custom middleware used by the application.
4+
* Custom middleware used by the application
55
*/
66
module.exports = {
77

Diff for: templates/express/mongo/thing.js renamed to templates/express/models/thing.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
var mongoose = require('mongoose'),
44
Schema = mongoose.Schema;
55

6-
// Schema
6+
/**
7+
* Thing Schema
8+
*/
79
var ThingSchema = new Schema({
810
name: String,
911
info: String,
1012
awesomeness: Number
1113
});
1214

13-
// Validations
15+
/**
16+
* Validations
17+
*/
1418
ThingSchema.path('awesomeness').validate(function (num) {
1519
return num >= 1 && num <= 10;
1620
}, 'Awesomeness must be between 1 and 10');
File renamed without changes.

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

-20
This file was deleted.

Diff for: templates/express/routes.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
var api = require('./controllers/api'),
4+
index = require('./controllers')<% if(mongoPassportUser) { %>,
5+
users = require('./controllers/users'),
6+
session = require('./controllers/session');
7+
8+
var middleware = require('./middleware')<% } %>;
9+
10+
/**
11+
* Application routes
12+
*/
13+
module.exports = function(app) {
14+
15+
// Server API Routes
16+
app.get('/api/awesomeThings', api.awesomeThings);
17+
<% if(mongoPassportUser) { %>
18+
app.post('/api/users', users.create);
19+
app.put('/api/users', users.changePassword);
20+
app.get('/api/users/me', users.me);
21+
app.get('/api/users/:id', users.show);
22+
23+
app.post('/api/session', session.login);
24+
app.del('/api/session', session.logout);<% } %>
25+
26+
// Default all other routes to use Angular Routing
27+
app.get('/partials/*', index.partials);
28+
app.get('/*',<% if(mongoPassportUser) { %> middleware.setUserCookie,<% } %> index.index);
29+
};

0 commit comments

Comments
 (0)