Categories
JavaScript Answers

How to store DB config in a Node.js and Express app?

Spread the love

To store DB config in a Node.js and Express app, we store it in an .env file.

For instance, we write

DATABASE_PASSWORD=pw
DATABASE_NAME=some_db

in our .env file.

Then we write

require("dotenv").config();
const password = process.env.DATABASE_PASSWORD;

to load the .env file with the dotenv package.

And then we get the DATABASE_PASSWORD environment variable value with process.env.DATABASE_PASSWORD.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *