从中间件访问Loopback配置数据

时间:2016-10-15 00:43:26

标签: node.js loopbackjs

假设我们在Loopback中间件中,例如

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

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

});

我想访问Loopback正在使用的整体配置。

我试过了:

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

什么都没有给我我想要的东西。

有趣的是,这给了我:

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

所以这是发生了什么的线索,但我想得到上述数据的父对象。

我们如何访问Loopback已加载的配置。当然,配置因环境变量等而异。

我想记录加载了哪些datasources.x.js文件以及加载了什么config.x.js文件,以及我可以捕获的任何其他服务器配置信息。

在弄清楚如何做到这一点时遇到了很多麻烦。

这似乎与我的问题相同: https://github.com/strongloop/loopback/issues/1526

但是他们指出了Google网上论坛的空白,我在那里搜索过,无法找到这个问题的答案。

1 个答案:

答案 0 :(得分:2)

此行为实际上是从Express继承的。

整个配置存储在app.settings对象中,app.get(key)app.set(key,value)仅作为getter / setter。

在新的环回安装上执行console.log(app.settings);(例如在server/server.js中)它会返回以下内容:

{ '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/' }