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的值
(一般是development或
production
); 例如:config.production.json
.
例如:
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
:
loopback.rest({disableStackTrace: true})
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:
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
.
Example data sources:
{ // 键是数据源的名字 // 值是传给app.dataSource(name, config)的config对象 db: { connector: 'memory' } }
{ 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
.
- overview
- typically, for all apps, you need to set up configs for different environments
- prod, dev, staging, etc
- we support two types of configs: app and datasource
- configure app settings via config.json LINK TO app init default files section config.json file
- configure datasource settings via datasource.json (and .js versions) LINK TO app init default files section datasources.json file
- turning off stack traces is a good idea LINK TO stack trace section
- disabling the API explorer is a good idea LINK TO disabling api explorer
- you can also retrieve values from ENV_VARS LINK TO env vars
- typically, for all apps, you need to set up configs for different environments
- app wide configs
- talk about config + datasource.json (ie app configs are set in config, ds is used to set ds configs at application level)
- overriding values in config.json (to override blah, do this)
- overrding values in datasources.json (to override blah, do this)
- turning off stack traces
- disabling the api explorer
- getting values from env vars