diff --git a/templates/components/api-server/component.js b/templates/components/api-server/component.js index 36e7d0a0..82a53d82 100644 --- a/templates/components/api-server/component.js +++ b/templates/components/api-server/component.js @@ -14,7 +14,7 @@ template.package = { "compression": "^1.0.3", "errorhandler": "^1.1.1", "loopback": "^2.8.0", - "loopback-boot": "^2.4.0", + "loopback-boot": "^2.6.5", "loopback-datasource-juggler": "^2.7.0", "serve-favicon": "^2.0.1" }, diff --git a/templates/components/api-server/template/server/server.js b/templates/components/api-server/template/server/server.js index daf536f5..a168b0af 100644 --- a/templates/components/api-server/template/server/server.js +++ b/templates/components/api-server/template/server/server.js @@ -3,10 +3,6 @@ var boot = require('loopback-boot'); var app = module.exports = loopback(); -// Bootstrap the application, configure models, datasources and middleware. -// Sub-apps like REST API are mounted via boot scripts. -boot(app, __dirname); - app.start = function() { // start the web server return app.listen(function() { @@ -15,7 +11,12 @@ app.start = function() { }); }; -// start the server if `$ node server.js` -if (require.main === module) { - app.start(); -} +// Bootstrap the application, configure models, datasources and middleware. +// Sub-apps like REST API are mounted via boot scripts. +boot(app, __dirname, function(err) { + if (err) throw err; + + // start the server if `$ node server.js` + if (require.main === module) + app.start(); +}); diff --git a/test/end-to-end.js b/test/end-to-end.js index a7417c31..6aca487d 100644 --- a/test/end-to-end.js +++ b/test/end-to-end.js @@ -140,6 +140,19 @@ describe('end-to-end', function() { done(err); }); }); + + it('emits the `booted` event when booting is complete', function(done) { + var src = FIXTURES + '/async.js'; + var dest = SANDBOX + '/server/boot/async.js'; + fs.copySync(src, dest); + delete require.cache[require.resolve(SANDBOX)]; + var app = require(SANDBOX); + app.on('booted', function() { + expect(app.asyncBoot, 'app.asyncBoot').to.be.true(); + done(); + }); + // the test will time out if `booted` is not emitted + }); }); describe('autoupdate', function() { diff --git a/test/fixtures/async.js b/test/fixtures/async.js new file mode 100644 index 00000000..47346810 --- /dev/null +++ b/test/fixtures/async.js @@ -0,0 +1,4 @@ +module.exports = function(app, cb) { + app.asyncBoot = typeof cb === 'function'; + process.nextTick(cb); +}; diff --git a/test/support.js b/test/support.js index 97d83fe3..9bb309cb 100644 --- a/test/support.js +++ b/test/support.js @@ -22,6 +22,7 @@ expectValueInJSONFile = function(file, propertyPath, val) { expect(obj).to.have.deep.property(propertyPath, val); } +FIXTURES = path.resolve(__dirname, 'fixtures/'); SANDBOX = path.resolve(__dirname, 'sandbox/'); // tell the workspace to load files from the sandbox