How does control flow from app.start() in MeanJS framework

73 Views Asked by At

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(); -

1

There are 1 best solutions below

2
Hamza Fatmi On

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 here var config = require('../config') (Line 6), the app parameter is declared here var app = express.init(db); (Line 25) and the db parameter is the result (database instance) coming from mongooseService.connect call (Line 20).