Step 7

Adding config

Let's create a config file to hold all of our application configurations.

$ npm i -S dotenv

src/config/index.js

require('dotenv').config();

const env = process.env;

const config = {
	PORT: env.PORT || 5000,
	SECRET: env.SECRET || 'my-secret',
	EXPIRES_IN: env.EXPIRES_IN || '7 days',
	NODE_ENV: env.NODE_ENV || 'development'
};

module.exports = config;

The secret and expires variables will be used in the next step. Now, we can replace our PORT variable with our config.

src/index.js

const express = require('express');
const { PORT } = require('./config');
const books = require('./routes/books');

const app = express();

app.use(express.json());
...

Last updated

Was this helpful?