NodeJs and react environments commonly require config parameters to be used. To implement this, use of dotenv
dependency is common. But spreading environment variables and dependencies in the codebase can lead to cluttered and inflexible codebase. Instead, having all the config responsibilities in one file can unify and standardise our config. As a result, it brings simplicity, flexibility and validation thanks to typescript.
Below is an example how I have setup my config in both my Node API environment and react frontend too.
const dotenv = require('dotenv');
dotenv.config();
const appRoot = require('app-root-path');
export type configInfo = {
port: number;
frontendUrl: string;
woocommerce: {
apiUrl: string,
apiKey: string,
apiSecret: string
webhookSecret: string
},
rootDir: string,
cache: {
redis: {
host: string,
port: number,
username: string,
password: string
}
}
}
export const config: configInfo = {
port: (process.env.PORT === undefined)? 8080: Number(process.env.PORT),
frontendUrl: (process.env.FRONTEND_URL === undefined)?'http://localhost:3001':process.env.FRONTEND_URL,
woocommerce: {
apiUrl: (process.env.WOOMMERCE_API_URL === undefined)?'localhost':process.env.WOOMMERCE_API_URL,
apiKey: (process.env.WOOCOMMERCE_KEY === undefined)?'dddfsaafdsg': process.env.WOOCOMMERCE_KEY,
apiSecret: (process.env.WOOCOMMERCE_SECRET === undefined)? 'rifjrjr': process.env.WOOCOMMERCE_SECRET,
webhookSecret: (process.env.WEBHOOK_SECRET === undefined)? 'fggfdhdth': process.env.WEBHOOK_SECRET,
},
rootDir: appRoot.resolve('/'),
cache: {
redis: {
host: (process.env.REDIS_HOST === undefined)? 'localhost': process.env.REDIS_HOST,
port: (process.env.REDIS_PORT === undefined)? 6379: Number(process.env.REDIS_PORT),
username: (process.env.REDIS_USERNAME === undefined)? 'default': process.env.REDIS_USERNAME,
password: (process.env.REDIS_PASSWORD === undefined)? 'dlldde': process.env.REDIS_PASSWORD,
}
}
}