Categories
JavaScript Answers

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

Spread the love

Sometimes, we want to store database config in Node.js and Express app.

In this article, we’ll look at how to store database config in Node.js and Express app.

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

To store database config in Node.js and Express app, we can use the dotenv module.

To install it, we run

npm i dotenv

Then we use it by writing

app.js

require('dotenv').config()

in our app’s entry point file.

And then in our .env file, we write

DATABASE_PASSWORD=mypw
DATABASE_NAME=some_db

Then we can use the environment variable values from process.env like

process.env.DATABASE_PASSWORD

Conclusion

To store database config in Node.js and Express app, we can use the dotenv module.

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 *