I am trying to play around with the MEAN framework. I used their scaffolding available here. But I am getting confused on how the control flow happens from the time we start the server.js [node server.js] file.
Adding snippets of some files, the entire code is here
./server.js
var app = require('./config/lib/app');
var server = app.start();
./config/lib/app.js
module.exports.start = function start(callback) {
var _this = this;
_this.init(function (app, db, config) {
// Start the app by listening on <port> at <host>
app.listen(config.port, config.host, function () {
// Create server URL
var server = (process.env.NODE_ENV === 'secure' ? 'https://' : 'http://') + config.host + ':' + config.port;
// Logging initialization
console.log('--');
.....
if (callback) callback(app, db, config);
});
});
};
For eg, where do the parameters come from in _this.init(function (app, db, config){ segment when im only calling app.start(); -
The
function (app, db, config){...}in_this.init(function (app, db, config){...}is just the function declaration, nothing is executed yet, the execution is done here (Line 26)if (callback) callback(app, db, config);where the passed config parameter is the global variable declared herevar config = require('../config')(Line 6), theappparameter is declared herevar app = express.init(db);(Line 25) and thedbparameter is the result (database instance) coming frommongooseService.connectcall (Line 20).