LogoLogo
  • Introduction
  • Backend
    • Creating a MongoDB Database
    • Environment Variables
    • Configuring the Mailing Service
    • Seeding the Database
    • Starting the Backend
  • Frontend
    • Starting the Frontend
  • Book Module Tutorial
    • Introduction
    • Backend
      • Create Model
      • Create Service
      • Create Route
      • Test using Postman
    • Frontend
      • Introduction
      • File Structure
      • Defining Concepts
      • Display App in Dashboard
      • Create Module
      • Create Book List Component
      • Create Main Component
      • Create Service
      • Display Book List
      • Add a Book
        • Update Service
        • Create Component
        • Final Code Review
      • Delete a Book
        • Update Service
        • Update Component
        • Final Code Review
      • Conclusion
  • Resources
  • Beginner
  • Advanced
  • Recommended Extensions for Visual Studio Code
  • Troubleshooting
    • Troubleshooting
Powered by GitBook
On this page
  • Introduction
  • Environment Variables in Node.js
  • Connection to the Database

Was this helpful?

Export as PDF
  1. Backend

Environment Variables

PreviousCreating a MongoDB DatabaseNextConfiguring the Mailing Service

Last updated 4 years ago

Was this helpful?

Introduction

Environment variables allow us to define some configuration that is decoupled from the code-base. It makes the application deployment easier in different environments (e.g. Heroku). Information that varies (hence the term variables) depending on the environment should be included in this file. Also, sensitive information like API tokens or database URLs should not be in this source code or known by unauthorized entities.

Environment Variables in Node.js

Node.js provides a global variable called process.env which is an that contains all environment variables accessible to the developer. These environment variables are stored in a file called .env and, by convention, they are defined in a capitalized snake case, e.g DB_URL.

If you check the repository, you will find a file called, .env.example that contains variables used by the backend application.

Connection to the Database

First, fork the repository and clone it on your machine (if you face some issues during this step, you can refer back to the GitHub Guide).

To allow the backend to connect to your database, create a .env file that is an exact copy of .env.example and modify it as follows:

# DB #
DB_URL = <CONNECTION-STRING-FROM-THE-PREVIOUS-SECTION>

Note that changes in the .env are ignored by git and that is why we provide the .env.example file to serve as a skeleton or a specification for the variables. As a matter of fact the .env file should not be pushed.

object