Alexander Mills Alexander Mills - 1 year ago 129
Node.js Question

Accessing Loopback config data from middleware

Say we are in Loopback middleware, such as


// 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:

=> '/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:

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 Source

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: '',
  port: 3000,
  restApiRoot: '/api',
   { 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/' }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download