How to set up a basic MERN stack project?
How to Set Up a Basic MERN Stack Project
The MERN stack is a popular JavaScript stack used for building full-stack web applications. It consists of:
-
MongoDB – Database
-
Express.js – Backend framework
-
React.js – Frontend framework
-
Node.js – Server runtime
Let’s go step-by-step to build a simple MERN stack project.
π§° Prerequisites
Make sure you have installed:
-
Visual Studio Code or any code editor
-
npm or yarn (comes with Node.js)
π Step 1: Create Project Folders
Now you have:
π§ Step 2: Set Up the Backend (Node.js + Express)
Navigate to backend/:
Create a file index.js:
Add a .env file:
Run the backend:
π§ Step 3: Create a MongoDB Model (Example: User)
Create a folder models and a file User.js inside it:
π― Step 4: Set Up API Routes
Create a folder routes and file userRoutes.js:
Then connect routes in index.js:
π Step 5: Set Up the Frontend (React)
Navigate to frontend/:
Edit App.js:
π Step 6: Run the App
Start Backend:
Start Frontend:
Visit: http://localhost:3000
You now have a working MERN Stack application!
✅ Summary
| Layer | Technology |
|---|---|
| Frontend | React.js |
| Backend | Node.js + Express |
| Database | MongoDB |
| Communication | REST API using Axios |
Comments
Post a Comment