Skip to content
This repository was archived by the owner on Aug 30, 2021. It is now read-only.

Commit edb6234

Browse files
committed
Refactoring server application service to enable modularity and callbacks, as well as provide app, db, config variables outside of server.js
1 parent 40878bb commit edb6234

File tree

4 files changed

+71
-36
lines changed

4 files changed

+71
-36
lines changed

config/lib/app.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict';
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
var config = require('../config'),
7+
mongoose = require('./mongoose'),
8+
express = require('./express'),
9+
chalk = require('chalk');
10+
11+
// Initialize Models
12+
mongoose.loadModels();
13+
14+
module.exports.loadModels = function loadModels() {
15+
mongoose.loadModels();
16+
};
17+
18+
module.exports.init = function init(callback) {
19+
20+
mongoose.connect(function (db) {
21+
// Initialize express
22+
var app = express.init(db);
23+
if (callback) callback(app, db, config);
24+
25+
});
26+
};
27+
28+
module.exports.start = function start(callback) {
29+
var _this = this;
30+
31+
_this.init(function(app, db, config) {
32+
33+
// Start the app by listening on <port>
34+
app.listen(config.port, function() {
35+
36+
// Logging initialization
37+
console.log('--');
38+
console.log(chalk.green(config.app.title));
39+
console.log(chalk.green('Environment:\t\t\t' + process.env.NODE_ENV));
40+
console.log(chalk.green('Port:\t\t\t\t' + config.port));
41+
console.log(chalk.green('Database:\t\t\t\t' + config.db.uri));
42+
if (process.env.NODE_ENV === 'secure') {
43+
console.log(chalk.green('HTTPs:\t\t\t\ton'));
44+
}
45+
console.log('--');
46+
47+
if (callback) callback(app, db, config);
48+
});
49+
50+
});
51+
52+
};

config/lib/mongoose.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ module.exports.connect = function(cb) {
3030
// Enabling mongoose debug mode if required
3131
mongoose.set('debug', config.db.debug);
3232

33-
// Load modules
34-
_this.loadModels();
35-
3633
// Call callback FN
3734
if (cb) cb(db);
3835
}

gruntfile.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,17 +234,30 @@ module.exports = function (grunt) {
234234
});
235235
});
236236

237+
grunt.task.registerTask('server', 'Starting the server', function() {
238+
// Get the callback
239+
var done = this.async();
240+
241+
var path = require('path');
242+
var app = require(path.resolve('./config/lib/app'));
243+
var server = app.start(function() {
244+
done();
245+
});
246+
247+
});
248+
249+
250+
237251
// Lint CSS and JavaScript files.
238252
grunt.registerTask('lint', ['sass', 'less', 'jshint', 'csslint']);
239253

240254
// Lint project files and minify them into two production files.
241255
grunt.registerTask('build', ['env:dev', 'lint', 'ngAnnotate', 'uglify', 'cssmin']);
242256

243257
// Run the project tests
244-
grunt.registerTask('test', ['env:test', 'lint', 'mkdir:upload', 'copy:localConfig', 'mongoose', 'mochaTest', 'karma:unit']);
245-
grunt.registerTask('test:server', ['env:test', 'lint', 'mongoose', 'mochaTest']);
246-
grunt.registerTask('test:client', ['env:test', 'lint', 'mongoose', 'karma:unit']);
247-
258+
grunt.registerTask('test', ['env:test', 'lint', 'mkdir:upload', 'copy:localConfig', 'server', 'mochaTest', 'karma:unit']);
259+
grunt.registerTask('test:server', ['env:test', 'lint', 'server', 'mochaTest']);
260+
grunt.registerTask('test:client', ['env:test', 'lint', 'server', 'karma:unit']);
248261
// Run the project in development mode
249262
grunt.registerTask('default', ['env:dev', 'lint', 'mkdir:upload', 'copy:localConfig', 'concurrent:default']);
250263

server.js

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,5 @@
33
/**
44
* Module dependencies.
55
*/
6-
var config = require('./config/config'),
7-
mongoose = require('./config/lib/mongoose'),
8-
express = require('./config/lib/express'),
9-
chalk = require('chalk');
10-
11-
/**
12-
* Main application entry file.
13-
* Please note that the order of loading is important.
14-
*/
15-
16-
// Initialize mongoose
17-
mongoose.connect(function (db) {
18-
// Initialize express
19-
var app = express.init(db);
20-
21-
// Start the app by listening on <port>
22-
app.listen(config.port);
23-
24-
// Logging initialization
25-
console.log('--');
26-
console.log(chalk.green(config.app.title));
27-
console.log(chalk.green('Environment:\t\t\t' + process.env.NODE_ENV));
28-
console.log(chalk.green('Port:\t\t\t\t' + config.port));
29-
console.log(chalk.green('Database:\t\t\t\t' + config.db.uri));
30-
if (process.env.NODE_ENV === 'secure') {
31-
console.log(chalk.green('HTTPs:\t\t\t\ton'));
32-
}
33-
console.log('--');
34-
});
6+
var app = require('./config/lib/app');
7+
var server = app.start();

0 commit comments

Comments
 (0)