Chinese Documentation : Environment-specific configuration

Overview

You can set up configurations for specific environments (for example, development, staging, and production) using config.json and datasources.json (and their corresponding JavaScript files).

Example

For an example, see https://github.com/strongloop/loopback-example-full-stack/tree/master/server.

For application configuration:

For data source configuration:

Application-wide configuration

可以在下面的文件中覆盖config.json中的配置:

  • config.local.js 或 config.local.json
  • config.env.js 或 config.env.json, 这里的env 是NODE_ENV的值 (一般是developmentproduction); 例如:config.production.json.
Icon

这些文件只能使用值类型(字符串,数字)来覆盖顶层的属性。目前还不支持聚合对象和数组。

例如:

config.production.js
module.exports = {
  host: process.env.CUSTOM_HOST,
  port: process.env.CUSTOM_PORT
};

Turning off stack traces

By default, stack traces are returned in JSON responses.  To turn this off, add this line to server/config.json:

server/config.json
loopback.rest({disableStackTrace: true})
Icon

Setting the NODE_ENV environment variable to "production" also turns off stack traces in JSON responses.

Disabling API Explorer

LoopBack API Explorer is great when you're developing your application, but for security reasons you may not want to expose it in production.  To disable API Explorer entirely, if you created your application with the Application generator, simply delete or rename server/boot/explorer.js

Customizing REST error handling

You can customize the REST error handler by adding the error handler callback function to config.local.js as follows:

server/config.js
module.exports = {
  remoting: {
    errorHandler: {
      handler: function(err, req, res, next) {
        // custom error handling logic
        // call `next()` to fall back to the default error handler
      }
    }
  }
};

Data source configuration

你可以在洗面的文件中覆盖datasource.json中设置的值:

  • datasources.local.js 或 datasources.local.json
  • datasources.env.js 或 datasources.env.json, 这里的env 是NODE_ENV环境变量的值。 (一般是development 或 production);例如,datasources.production.json.
Icon

这些文件只能使用值类型(字符串,数字)来覆盖顶层的属性。目前还不支持聚合对象和数组。

Example data sources:

datasources.json
{
  // 键是数据源的名字
  // 值是传给app.dataSource(name, config)的config对象
  db: {
    connector: 'memory'
  }
} 
datasources.production.json
{
  db: {
    connector: 'mongodb',
    database: 'myapp',
    user: 'myapp',
    password: 'secret'
  }
}

Getting values from environment variables

You can easily set an environment variable when you run an application like this:

$ MY_CUSTOM_VAR="some value" slc run

or

$ export MY_CUSTOM_VAR="some value"
$ slc run

Then this variable is available to your application as process.env.MY_CUSTOM_VAR.

Non-public Information
  1. overview
    1. typically, for all apps, you need to set up configs for different environments
      1. prod, dev, staging, etc
      2. we support two types of configs: app and datasource
        1. configure app settings via config.json LINK TO app init default files section config.json file
        2. configure datasource settings via datasource.json (and .js versions) LINK TO app init default files section datasources.json file
    2. turning off stack traces is a good idea LINK TO stack trace section
    3. disabling the API explorer is a good idea LINK TO disabling api explorer
    4. you can also retrieve values from ENV_VARS LINK TO env vars
  2. app wide configs
    1. talk about config + datasource.json (ie app configs are set in config, ds is used to set ds configs at application level)
    2. overriding values in config.json (to override blah, do this)
    3. overrding values in datasources.json (to override blah, do this)
  3. turning off stack traces
  4. disabling the api explorer
  5. getting values from env vars