Application configuration in Express
There are various things to be configured in the web application. I just started to test various databases and I wanted to have a place to store my database configuration properly. I also wanted to have separate production and development configurations.
Directories and files
I started by creating the directory named config in the root folder of my project. Because I want to have separate configuration for dev and prod, and there will be most likely some values shared between these two, I created three files. In /config/config.global.js I placed the default values for two of my configuration variables:
var config = module.exports = {}; config.env = 'undefined'; config.hostname = 'undefined.example.com';
This way I was able to include default variables in my other configuration files. The first one will be used in dev environment, I named it /config/config.development.js and filled with the data as follows:
var config = require('./config.global'); config.env = 'dev'; config.hostname = 'localhost:3000'; config.db = { database: 'null', username: 'null', password: 'null', sequelizeParams: { dialect: 'sqlite', storage: './sqliteDB/friendlyTracker.sqlite', operatorsAliases: false } } module.exports = config;
As you can see, I placed the configuration for Sequelize ORM to use SQLite database and I also changed the default values of my hostname and env variables. The same way I created the production-related configuration file and named it /config/config.prod.js. Because I don’t want to store my configuration in Git, I also updated .gitignore file with the following lines:
# Configuration files should not be stored in GIT /config/config.development.js /config/config.prod.js
As the last step, because development and production configuration files will not be visible in Git, I created sample configuration file to be able to quickly create prod and dev configurations. In the /config/config.sample.js I simply copied the contents of development configuration and adjusted values:
var config = require('./config.global'); config.env = 'dev'; config.hostname = 'dev.example'; config.db = { database: 'null', username: 'null', password: 'null', sequelizeParams: { dialect: 'sqlite', storage: './sqliteDB/friendlyTracker.sqlite', operatorsAliases: false } } module.exports = config;
With the above files, I have my configuration ready to be used.
Application adjustments
It is enough to load my application configuration during the application start. This means that I can store configuration in the application scope and use it when needed. Because these configuration variables will not be used on Handlebars layouts, I don’t need to store it in app.locals – I decided to store it using app.set. (In my other article you can find more about the variables and passing them through middleware). The changes I made to my app.js file are rather simple:
//... var env = process.env.NODE_ENV || 'development' , config = require('./config/config.'+env); //... app.set('config',config); //...
Now, using my configuration variables, I’m able to create SQLite connection using Sequelize in my middleware:
var cfg = req.app.get('config'); var con = new Sequelize(cfg.db.database, cfg.db.username, cfg.db.password, cfg.db.sequelizeParams);
This is a very simple use case for my configuration, but it will most likely grow with the project.
You can find this project source on GitHub.