Logo

dev-resources.site

for different kinds of informations.

The MERN stack series !

Published at
11/12/2024
Categories
react
node
express
mern
Author
bilalkhanio
Categories
4 categories in total
react
open
node
open
express
open
mern
open
Author
11 person written this
bilalkhanio
open
The MERN stack series !

Post 2: Setting Up Your MERN Stack Development Environment

In our first post, we introduced the MERN stack and discussed why it’s a powerful framework for building full-stack web applications. Now, let’s dive into setting up your development environment so you’re ready to start building!

1. Installing Node.js and npm

Node.js is the backbone of your MERN stack, as it allows JavaScript to run on the server side. When you install Node.js, it includes npm (Node Package Manager), which you’ll use to manage libraries and dependencies for both the backend and frontend.

  • Download Node.js: Visit nodejs.org and download the latest stable version. Follow the installation instructions.
  • Verify Installation: Open your terminal and type:
  node -v
  npm -v
Enter fullscreen mode Exit fullscreen mode

You should see the version numbers if the installation was successful.

2. Setting Up MongoDB

MongoDB is where your data will be stored. You have two options for setting up MongoDB: install it locally, or use MongoDB Atlas (a cloud database).

  • Option A: Local MongoDB Installation: Download MongoDB Community Edition from mongodb.com and follow the setup instructions. Run the MongoDB service by typing:
  mongod
Enter fullscreen mode Exit fullscreen mode

This starts MongoDB on your local machine.

  • Option B: MongoDB Atlas: Sign up at mongodb.com/cloud/atlas and create a new cluster. Atlas provides a URI connection string, which you’ll use later to connect your backend to MongoDB.

3. Creating the Project Structure

In this series, we’ll organize the project into two main directories: backend for the server (Express/Node) and frontend for the client (React). Let’s start setting up the structure.

  1. Create a Folder for Your Project:
   mkdir mern-project
   cd mern-project
Enter fullscreen mode Exit fullscreen mode
  1. Set Up the Backend:

    • Create a backend folder:
     mkdir backend
     cd backend
    
  • Initialize npm and install dependencies:

     npm init -y
     npm install express mongoose dotenv
    
    • Express: The backend framework for creating server routes.
    • Mongoose: A MongoDB object modeling tool for managing schemas.
    • dotenv: Allows us to use environment variables (e.g., MongoDB URI).
  • Create the main entry file for the server:

     touch index.js
    
  1. Set Up the Frontend:

    • In the mern-project directory, create a frontend folder:
     cd ..
     npx create-react-app frontend
    

    This will scaffold a basic React app inside the frontend folder. Verify it works by running:

     cd frontend
     npm start
    
  • Your React app should open in the browser at http://localhost:3000.

4. Writing a Simple Express Server

With the backend and frontend directories in place, let’s get our server running. Go to the backend folder and edit index.js:

// backend/index.js
const express = require("express");
const mongoose = require("mongoose");
const dotenv = require("dotenv");

dotenv.config();

const app = express();
const PORT = process.env.PORT || 5000;

app.get("/", (req, res) => {
  res.send("Hello from the MERN backend!");
});

app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
Enter fullscreen mode Exit fullscreen mode

Now add a .env file in the backend folder with any environment variables (e.g., MONGO_URI if using MongoDB Atlas). Run the server with:

node index.js
Enter fullscreen mode Exit fullscreen mode

5. Connecting MongoDB to Express with Mongoose

In your index.js, add a connection to MongoDB using Mongoose.

mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true })
  .then(() => console.log("Connected to MongoDB"))
  .catch(err => console.log(err));
Enter fullscreen mode Exit fullscreen mode

Replace process.env.MONGO_URI with your actual MongoDB URI if you’re not using a .env file.

Next Steps

Now that we have our environment set up, we’re ready to start building a REST API with Express, which will let our frontend React app interact with MongoDB. Stay tuned for Post 3, where we’ll cover MongoDB basics and build CRUD functionality on the backend!

mern Article's
30 articles in total
Favicon
Building RelaxTube: A Scalable Video Transcoding and Streaming Application
Favicon
Does anyone have experience deploying a #MERN application in production on a #DigitalOcean droplet, using a domain name from #GoDaddy, and setting up an email server with #Hostinger? I would appreciate any guidance or best practices for handling this setup
Favicon
Does anyone have experience deploying a MERN application in production on a DigitalOcean droplet, using a domain name from GoDaddy, and setting up an email server with Hostinger? I would appreciate any guidance or best practices for handling this setup
Favicon
Getting Started with MERN Stack: A Beginner's Guide
Favicon
Google Authentication in MERN Stack
Favicon
Bootcamp vs. Self-Taught: Which path is the best?
Favicon
How to Build and Deploy a Full-Stack MERN Application
Favicon
Blogsphere | A blogging website made with MERN stack. includes user management.
Favicon
A question to start
Favicon
Mastering Node.js Routing: A Complete Guide with Internal Workings Explained
Favicon
How it started...
Favicon
How I Built My First MERN Project: Challenges, Solutions, and Lessons Learned
Favicon
I have 2 questions about web dev and mern stack
Favicon
Hackers Love These Common MERN Stack Mistakes: Are You Exposing Your App? 🔐
Favicon
mern stack course
Favicon
Best Practices for Building Scalable Web Applications with the MERN Stack
Favicon
The MERN stack series !
Favicon
Why React Can Surprise You (And How to Tame It)
Favicon
3 Reasons Why you should go to the university instead of learn by yourself
Favicon
Simplify Form Handling in Your MERN Stack Projects with Formik
Favicon
Top 7 Reasons to Hire a MERN Stack Development Company for Scalable Web Solutions
Favicon
The Advantages of the MERN Stack for Full-Stack Development
Favicon
FSD 1.0 - Introduction to Full Stack Development
Favicon
Dear Past Me: React Lessons from the Future
Favicon
The MERN stack series !
Favicon
The MERN stack series !
Favicon
The Truth About Prototypes in JavaScript: Flexibility vs. Performance
Favicon
Boost Your Productivity: React.js Is Magic: How It Changed the Game for Developers 🎩✨
Favicon
Interactive Portfolio Website
Favicon
Yarn vs Bun to Create Vite Project....

Featured ones: