const app = require('./config/app.js')
console.log(app.appKey)
You are viewing the legacy version of AdonisJS. Visit https://adonisjs.com for newer docs. This version will receive security patches until the end of 2021.
The first step towards removing the spaghetti code is to find a dedicated place for storing application configuration. AdonisJs makes use of the config
directory for same. Every new project created using adonis-cli
comes with a bunch of pre-configured config files. Also, you are free to create your application config files inside the same directory.
To keep it straightforward and DRY, AdonisJs has an inbuilt Config Provider which autoloads all the configuration files (ending with .js) at the time of booting the server which means you have access to values from all the files in the config
directory.
Do make sure not to manually require config files inside your app and instead take advantage of the Config provider.
const app = require('./config/app.js')
console.log(app.appKey)
const Config = use('Config')
console.log(Config.get('app.appKey'))
Reading/writing values is a fairly simple task with the help of the Config provider. Also, you can make use of dot notation to get/set values.
get
method is used to read values. Also it accepts an optional defaultValue
, which is returned when the actual value is undefined
or null
.
const Config = use('Config')
Config.get('database.host', 'localhost')
set
method will update the existing value with the new value. Also it will create the key/value pair if it does not exists.
const Config = use('Config')
Config.set('database.host', '127.0.0.1')