Alexander Mills Alexander Mills - 1 month ago 12
Node.js Question

Accessing Loopback config data from middleware

Say we are in Loopback middleware, such as

app.use(function(req,res,next){

// I am not sure what key to use here in the getter...
const config = app.get('?');

});


I want to access the overall config that Loopback is using.

I tried:

const config = app.get('config');
const config = app.get('env');
const config = app.get('datasources');


nothing gives me quite what I want.

Interestingly, this gives me:

console.log(app.get('restApiRoot'));
=> '/api'


so that's a clue to what's going on, but I want to get the parent object(s) for the above data.

how can we access the configuration that Loopback has loaded. The configuration of course varies by environment variables etc.

I want to log what datasources.x.js file was loaded and what config.x.js file was loaded, and any other server configuration info I can capture.

Having a lot of trouble figuring out how to do this.

This seems to be the same question I have:
https://github.com/strongloop/loopback/issues/1526

but they point me to the void that is Google Groups and I searched through there and couldn't find what the answer to this question.

Answer

This behavior is actually inherited from Express.

The entire config is stored in the app.settings object, with app.get(key) and app.set(key,value) just acting as getter/setter.

Doing console.log(app.settings); (in server/server.js for instance) it on a fresh loopback install returns the following:

{ 'x-powered-by': true,
  etag: 'weak',
  'etag fn': [Function: wetag],
  env: 'development',
  'query parser': 'extended',
  'query parser fn': [Function: parseExtendedQueryString],
  'subdomain offset': 2,
  'trust proxy': false,
  'trust proxy fn': [Function: trustNone],
  view: [Function: View],
  views: 'C:\\Users\\*******\\Documents\\GitHub\\lbtest\\views',
  'jsonp callback name': 'callback',
  host: '0.0.0.0',
  port: 3000,
  restApiRoot: '/api',
  remoting:
   { context: { enableHttpContext: false },
     rest: { normalizeHttpPath: false, xml: false },
     json: { strict: false, limit: '100kb' },
     urlencoded: { extended: true, limit: '100kb' },
     cors: false,
     errorHandler: { disableStackTrace: false } },
  legacyExplorer: false,
  'loopback-component-explorer': { mountPath: '/explorer' },
  url: 'http://localhost:3000/' }
Comments