4.4 KiB
| outline |
|---|
| deep |
Configuration
npm install @feathersjs/configuration --save
@feathersjs/configuration is a wrapper for node-config which allows to configure a server side Feathers application.
By default this implementation will look in config/* for default.json. It will be merged with other configuration files in the config/ folder using the NODE_ENV environment variable. So setting NODE_ENV=production will merge config/default.json with config/production.json.
For more information refer to the node-config docs.
Usage
The @feathersjs/configuration module is an app configuration function that takes a root directory (usually something like __dirname in your application) and the configuration folder (set to config by default):
import { feathers } from '@feathersjs/feathers'
import configuration from '@feathersjs/configuration'
// Use the application root and `config/` as the configuration folder
const app = feathers().configure(configuration())
Direct access to nested config properties is not supported via
app.get(). To access a nested config property (e.g.Customer.dbConfig.host, useapp.get('Customer').dbConfig.hostorimport config from 'config'directly and use it as documented.
Configuration schema
The application configuration can be validated against a Feathers schema when app.setup (or app.listen) is called by passing a schema when initializing @feathersjs/configuration:
import { feathers } from '@feathersjs/feathers'
import { schema, type Infer } from '@feathersjs/schema'
import configuration from '@feathersjs/configuration'
const configurationSchema = schema({
$id: 'FeathersConfiguration',
type: 'object',
additionalProperties: false,
required: ['port', 'host'],
properties: {
port: { type: 'number' },
host: { type: 'string' }
}
} as const)
type ServiceTypes = {}
// Use the schema type for typed `app.get` and `app.set` calls
type Configuration = Infer<typeof configurationSchema>
// Use the application root and `config/` as the configuration folder
const app = feathers<ServiceTypes, Configuration>().configure(
configuration(configurationSchema)
)
// Configuration will only be validated now
app.listen()
.then(() => console.log('Server started'))
.catch((error) => {
// Configuration validation errors will show up here
})
Environment variables
As recommended by node-config, it is possible to override the configuration with custom variables by passing a JSON object in the NODE_CONFIG environment variable:
$ export NODE_CONFIG='{ "port": 8080, "host": "production.app" }'
$ node myapp.js
Individual environment variables can be used through Custom Environment Variables by creating a config/custom-environment-variables.json like this:
{
"port": "PORT",
"mongodb": "MONGOHQ_URL"
}
Configuration directory
By default, Feathers will use the config/ directory in the root of your project’s source directory. To change this, e.g., if you have Feathers installed under the server/ directory and you want your configuration at server/config/, you have to set the NODE_CONFIG_DIR environment variable in app.js before importing @feathersjs/configuration:
$ export NODE_CONFIG_DIR=server/config
$ node myapp.js
Note: The NODE_CONFIG_DIR environment variable isn’t used directly by @feathersjs/configuration but by the node-config module that it uses. For more information on configuring node-config settings, see the Configuration Files Wiki page.