How to Set Up Node.js, Express, and dotenv for Creating Routes: MERN Blog App Tutorial (Part 1)

Posted by






MERN Blog App Tutorial: Part 1 – Setting Up Node.js, Express, and dotenv to Create Routes

MERN Blog App Tutorial: Part 1 – Setting Up Node.js, Express, and dotenv to Create Routes

In this tutorial, we will be setting up the backend infrastructure for a MERN (MongoDB, Express, React, Node.js) blog application. We will start by setting up Node.js, Express, and dotenv to create routes for our application.

Prerequisites

Before we begin, make sure you have Node.js installed on your machine. You can download it from the official Node.js website. You will also need to have MongoDB installed and running as we will be using it as our database.

Setting Up Node.js

To get started, first create a new directory for your project and navigate into it using the terminal. Then, initialize a new Node.js project by running the following command:

npm init -y

This will create a new package.json file with default settings for your project.

Setting Up Express

Next, we need to install Express, a web framework for Node.js. In the terminal, run the following command to install Express as a dependency for your project:

npm install express

Once the installation is complete, create a new file called app.js in your project directory. This will be the entry point for your application. In app.js, create a basic Express server:

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

app.get('/', (req, res) => {
  res.send('Hello World!');
});

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

Now, you can run your Express server by running the following command in the terminal:

node app.js

If everything is set up correctly, you should see a message in the terminal saying “Server is running on port 3000”. You can then open a web browser and navigate to http://localhost:3000 to see the “Hello World!” message.

Setting Up dotenv

Lastly, we will install and set up dotenv to manage our environment variables. Environment variables are essential for securely storing sensitive information such as database credentials and API keys. In the terminal, run the following command to install dotenv as a dependency for your project:

npm install dotenv

After installing dotenv, create a new file called .env in your project directory. This is where you will store your environment variables. For example, you can define a variable for your database connection string:

        
DB_CONNECTION_STRING=mongodb://localhost:27017/myblogdb
        
    

In your app.js file, you can then use dotenv to load your environment variables:

        
require('dotenv').config();
const dbConnectionString = process.env.DB_CONNECTION_STRING;
        
    

Now you have successfully set up Node.js, Express, and dotenv for your MERN blog application. In the next part of this tutorial, we will be creating routes to handle different endpoints for our application.


0 0 votes
Article Rating
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Tooni Toon
11 months ago

Brother, The way of your explanation is very beautiful.