Add app.restApiRoot setting

Allow loopback users to configure API root via config file, instead of
editing app.js generated by loopback-workspace.

Allow loopback plugins to discover the path where the REST adapter is
mounted.
This commit is contained in:
Miroslav Bajtoš 2014-01-07 16:05:48 +01:00
parent 6ea8ad20fd
commit 92896a441c
2 changed files with 15 additions and 0 deletions

View File

@ -241,6 +241,11 @@ app.boot = function(options) {
app.get('port') || app.get('port') ||
3000; 3000;
appConfig.restApiRoot =
appConfig.restApiRoot ||
app.get('restApiRoot') ||
'/api';
if(appConfig.host !== undefined) { if(appConfig.host !== undefined) {
assert(typeof appConfig.host === 'string', 'app.host must be a string'); assert(typeof appConfig.host === 'string', 'app.host must be a string');
app.set('host', appConfig.host); app.set('host', appConfig.host);
@ -252,6 +257,11 @@ app.boot = function(options) {
app.set('port', appConfig.port); app.set('port', appConfig.port);
} }
assert(appConfig.restApiRoot !== undefined, 'app.restBasePath is required');
assert(typeof appConfig.restApiRoot === 'string', 'app.restBasePath must be a string');
assert(/^\//.test(appConfig.restApiRoot), 'app.restBasePath must start with "/"');
app.set('restApiRoot', appConfig.restBasePath);
for(var configKey in appConfig) { for(var configKey in appConfig) {
var cur = app.get(configKey); var cur = app.get(configKey);
if(cur === undefined || cur === null) { if(cur === undefined || cur === null) {

View File

@ -44,6 +44,7 @@ describe('app', function() {
app: { app: {
port: 3000, port: 3000,
host: '127.0.0.1', host: '127.0.0.1',
restApiRoot: '/rest-api',
foo: {bar: 'bat'}, foo: {bar: 'bat'},
baz: true baz: true
}, },
@ -71,6 +72,10 @@ describe('app', function() {
assert.equal(this.app.get('host'), '127.0.0.1'); assert.equal(this.app.get('host'), '127.0.0.1');
}); });
it('should have restApiRoot setting', function() {
assert.equal(this.app.get('restApiRoot'), '/rest-api');
});
it('should have other settings', function () { it('should have other settings', function () {
expect(this.app.get('foo')).to.eql({ expect(this.app.get('foo')).to.eql({
bar: 'bat' bar: 'bat'