,

Exploring Environment Variables in Express JS: A Comprehensive NODE JS Course

Posted by


In this tutorial, we will be covering environment variables and how to work with them in an Express.js application as part of our complete Node.js course series.

Environment variables are important in any application development because they allow you to store sensitive data such as API keys, database credentials, and other configuration settings without hardcoding them into your code. This helps keep your code secure and makes it easier to manage different configurations for different environments (such as development, testing, and production).

To get started, let’s first install Express.js in our project. Open your terminal and navigate to your project directory, then run the following command:

npm install express

Next, let’s create a new file called app.js and import the Express module:

const express = require('express');
const app = express();

Now, let’s set up a basic Express server that listens on port 3000:

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

If you run this code by executing node app.js in your terminal, you should see the message Server running on port 3000 printed to the console.

Now that we have our basic Express server set up, let’s move on to adding environment variables to our application. We can use the dotenv package to load environment variables from a .env file, so let’s install it by running the following command:

npm install dotenv

Next, create a new file called .env in your project’s root directory and add some environment variables to it:

PORT=3000
SECRET_KEY=mysecretkey

Now, let’s load these environment variables in our app.js file by requiring dotenv and calling config() on it:

require('dotenv').config();

You can now access these environment variables in your code using process.env.PORT and process.env.SECRET_KEY, like so:

const port = process.env.PORT || 3000;

app.listen(port, () => {
  console.log(`Server running on port ${port}`);
});

const secretKey = process.env.SECRET_KEY;
console.log(`Secret key: ${secretKey}`);

By default, the dotenv package looks for a .env file in your project’s root directory, but you can also specify a different path if needed. Make sure to add .env to your .gitignore file to prevent sensitive data from being exposed in your version control system.

That’s it! You now have a basic understanding of how to work with environment variables in an Express.js application. Feel free to explore more advanced use cases and configurations based on your project requirements.

I hope this tutorial was helpful, and I encourage you to continue learning and experimenting with Node.js and Express.js. Happy coding!

0 0 votes
Article Rating
8 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@hassannouri9796
2 months ago

in regard to setting node_env to development …​ well its the same ..just like that but in this case keep in mind when you setting in command line way you should put development( string) in single ir double quotation( $env:NODE_ENV="development"..this worked out right for me)

@hassannouri9796
2 months ago

❤❤❤

@littlewonder8641
2 months ago

😀😀😀

@srilekha8671
2 months ago

Sir, my PC doesn't accept set environment variables what should I do now

@sourabhlodhi1675
2 months ago

Sir me student ho sir 1 Q tha jab express se routing kar skate hai tou node me HTTP or URL module Ku banye video bana ke bata dijiye ga

@CHANDRASHEKHAR-hk8sq
2 months ago

Sir please add source code in below of the each video it's easy to practice sir

@ethiotech10
2 months ago

You are best at teaching! How about Nextjs? Thank you 🎉

@rishiraj2548
2 months ago

🙏🙏