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

Commit

Permalink
Refactoring server application service to enable modularity and callb…
Browse files Browse the repository at this point in the history
…acks, as well as provide app, db, config variables outside of server.js
  • Loading branch information
lirantal committed Jul 30, 2015
1 parent 40878bb commit edb6234
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 36 deletions.
52 changes: 52 additions & 0 deletions config/lib/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';

/**
* Module dependencies.
*/
var config = require('../config'),
mongoose = require('./mongoose'),
express = require('./express'),
chalk = require('chalk');

This comment has been minimized.

Copy link
@tirushma

tirushma Aug 3, 2016

//creating section

This comment has been minimized.

Copy link
@ilanbiala

ilanbiala Aug 9, 2016

Member

What is this comment?


// Initialize Models
mongoose.loadModels();

module.exports.loadModels = function loadModels() {
mongoose.loadModels();
};

module.exports.init = function init(callback) {

mongoose.connect(function (db) {
// Initialize express
var app = express.init(db);
if (callback) callback(app, db, config);

});
};

module.exports.start = function start(callback) {
var _this = this;

_this.init(function(app, db, config) {

// Start the app by listening on <port>
app.listen(config.port, function() {

// Logging initialization
console.log('--');
console.log(chalk.green(config.app.title));
console.log(chalk.green('Environment:\t\t\t' + process.env.NODE_ENV));
console.log(chalk.green('Port:\t\t\t\t' + config.port));
console.log(chalk.green('Database:\t\t\t\t' + config.db.uri));
if (process.env.NODE_ENV === 'secure') {
console.log(chalk.green('HTTPs:\t\t\t\ton'));
}
console.log('--');

if (callback) callback(app, db, config);
});

});

};
3 changes: 0 additions & 3 deletions config/lib/mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ module.exports.connect = function(cb) {
// Enabling mongoose debug mode if required
mongoose.set('debug', config.db.debug);

// Load modules
_this.loadModels();

// Call callback FN
if (cb) cb(db);
}
Expand Down
21 changes: 17 additions & 4 deletions gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,17 +234,30 @@ module.exports = function (grunt) {
});
});

grunt.task.registerTask('server', 'Starting the server', function() {
// Get the callback
var done = this.async();

var path = require('path');
var app = require(path.resolve('./config/lib/app'));
var server = app.start(function() {
done();
});

});



// Lint CSS and JavaScript files.
grunt.registerTask('lint', ['sass', 'less', 'jshint', 'csslint']);

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

// Run the project tests
grunt.registerTask('test', ['env:test', 'lint', 'mkdir:upload', 'copy:localConfig', 'mongoose', 'mochaTest', 'karma:unit']);
grunt.registerTask('test:server', ['env:test', 'lint', 'mongoose', 'mochaTest']);
grunt.registerTask('test:client', ['env:test', 'lint', 'mongoose', 'karma:unit']);

grunt.registerTask('test', ['env:test', 'lint', 'mkdir:upload', 'copy:localConfig', 'server', 'mochaTest', 'karma:unit']);
grunt.registerTask('test:server', ['env:test', 'lint', 'server', 'mochaTest']);
grunt.registerTask('test:client', ['env:test', 'lint', 'server', 'karma:unit']);
// Run the project in development mode
grunt.registerTask('default', ['env:dev', 'lint', 'mkdir:upload', 'copy:localConfig', 'concurrent:default']);

Expand Down
31 changes: 2 additions & 29 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,5 @@
/**
* Module dependencies.
*/
var config = require('./config/config'),
mongoose = require('./config/lib/mongoose'),
express = require('./config/lib/express'),
chalk = require('chalk');

/**
* Main application entry file.
* Please note that the order of loading is important.
*/

// Initialize mongoose
mongoose.connect(function (db) {
// Initialize express
var app = express.init(db);

// Start the app by listening on <port>
app.listen(config.port);

// Logging initialization
console.log('--');
console.log(chalk.green(config.app.title));
console.log(chalk.green('Environment:\t\t\t' + process.env.NODE_ENV));
console.log(chalk.green('Port:\t\t\t\t' + config.port));
console.log(chalk.green('Database:\t\t\t\t' + config.db.uri));
if (process.env.NODE_ENV === 'secure') {
console.log(chalk.green('HTTPs:\t\t\t\ton'));
}
console.log('--');
});
var app = require('./config/lib/app');
var server = app.start();

0 comments on commit edb6234

Please sign in to comment.