From 78c3b06cbe24ccdf49800ec17d0883d95399a766 Mon Sep 17 00:00:00 2001 From: crandmck Date: Tue, 29 Sep 2015 15:54:13 -0700 Subject: [PATCH] Upgrade LB Explorer and related changes --- example-2.0/package.json | 4 +--- example-2.0/server/boot/explorer.js | 23 ----------------------- example-2.0/server/boot/rest-api.js | 4 ---- example-2.0/server/component-config.json | 5 +++++ example-2.0/server/middleware.json | 3 +++ example-2.0/server/server.js | 23 +++++++++++++++-------- 6 files changed, 24 insertions(+), 38 deletions(-) delete mode 100644 example-2.0/server/boot/explorer.js delete mode 100644 example-2.0/server/boot/rest-api.js create mode 100644 example-2.0/server/component-config.json diff --git a/example-2.0/package.json b/example-2.0/package.json index 4a531aa..7f50487 100644 --- a/example-2.0/package.json +++ b/example-2.0/package.json @@ -10,11 +10,9 @@ "errorhandler": "^1.1.1", "loopback": "^2.0.0", "loopback-boot": "^2.0.0", + "loopback-component-explorer": "^2.1.0", "loopback-component-storage": "^1.5.0", "loopback-datasource-juggler": "^2.7.0", "serve-favicon": "^2.0.1" - }, - "optionalDependencies": { - "loopback-explorer": "^1.1.0" } } diff --git a/example-2.0/server/boot/explorer.js b/example-2.0/server/boot/explorer.js deleted file mode 100644 index b74d498..0000000 --- a/example-2.0/server/boot/explorer.js +++ /dev/null @@ -1,23 +0,0 @@ -module.exports = function mountLoopBackExplorer(server) { - var explorer; - try { - explorer = require('loopback-explorer'); - } catch(err) { - console.log( - 'Run `npm install loopback-explorer` to enable the LoopBack explorer' - ); - return; - } - - var restApiRoot = server.get('restApiRoot'); - - var explorerApp = explorer(server, { basePath: restApiRoot }); - server.use('/explorer', explorerApp); - server.once('started', function() { - var baseUrl = server.get('url').replace(/\/$/, ''); - // express 4.x (loopback 2.x) uses `mountpath` - // express 3.x (loopback 1.x) uses `route` - var explorerPath = explorerApp.mountpath || explorerApp.route; - console.log('Browse your REST API at %s%s', baseUrl, explorerPath); - }); -}; diff --git a/example-2.0/server/boot/rest-api.js b/example-2.0/server/boot/rest-api.js deleted file mode 100644 index 256579a..0000000 --- a/example-2.0/server/boot/rest-api.js +++ /dev/null @@ -1,4 +0,0 @@ -module.exports = function mountRestApi(server) { - var restApiRoot = server.get('restApiRoot'); - server.use(restApiRoot, server.loopback.rest()); -}; diff --git a/example-2.0/server/component-config.json b/example-2.0/server/component-config.json new file mode 100644 index 0000000..f36959a --- /dev/null +++ b/example-2.0/server/component-config.json @@ -0,0 +1,5 @@ +{ + "loopback-component-explorer": { + "mountPath": "/explorer" + } +} diff --git a/example-2.0/server/middleware.json b/example-2.0/server/middleware.json index 5a93e0f..a1be9a1 100644 --- a/example-2.0/server/middleware.json +++ b/example-2.0/server/middleware.json @@ -13,6 +13,9 @@ "parse": { }, "routes": { + "loopback#rest": { + "paths": ["${restApiRoot}"] + } }, "files": { "loopback#static": { diff --git a/example-2.0/server/server.js b/example-2.0/server/server.js index 53ba98e..3be7b08 100644 --- a/example-2.0/server/server.js +++ b/example-2.0/server/server.js @@ -3,18 +3,25 @@ var boot = require('loopback-boot'); var app = module.exports = loopback(); -// boot scripts mount components like REST API -boot(app, __dirname); - app.start = function() { // start the web server return app.listen(function() { app.emit('started'); - console.log('Web server listening at: %s', app.get('url')); + var baseUrl = app.get('url').replace(/\/$/, ''); + console.log('Web server listening at: %s', baseUrl); + if (app.get('loopback-component-explorer')) { + var explorerPath = app.get('loopback-component-explorer').mountPath; + console.log('Browse your REST API at %s%s', baseUrl, explorerPath); + } }); }; -// 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(); +});