βοΈ What is a Server?
A server is a program that:
Listens for client requests (like from your React app π§ )
Processes data πΎ
Sends responses back (like JSON, files, or HTML)
In modern web apps, we usually build servers using Node.js and Express.js because: β
Itβs JavaScript-based (same language as React)
π§© Step 1. Install Node.js
1. Go to π https://nodejs.org
2. Download and install the LTS version (recommended)
3. Verify installation by running this in the terminal:
node -v
npm -v
If you see version numbers, Node is installed β
π Step 2. Create a New Project Folder
mkdir my-server
cd my-server
npm init -y
This will create a package.json file for your project.
βοΈ Step 3. Install Express.js
Express helps you build servers easily.
npm install express
π§ Step 4. Create Your Server File
Create a file named server.js inside your project folder.
Then write this code π
// Import express
const express = require("express");
const app = express();
// Middleware to parse JSON data
app.use(express.json());
// Basic route
app.get("/", (req, res) => {
res.send("π Hello, Asfaque! Your server is running!");
});
// Start server on port 5000
app.listen(5000, () => {
console.log("β
Server started on http://localhost:5000");
});
βΆοΈ Step 5. Run the Server
Run this in your terminal:
node server.js
Output
β
Server started on http://localhost:5000
Now open your browser and visit π http://localhost:5000
Youβll see:
> π Hello, Asfaque! Your server is running!
π Congratulations β your first server is live!
π¬ Step 6. Add More Routes
You can add multiple routes (like /api, /users, etc.)
Example
app.get("/api/user", (req, res) => {
res.json({ name: "Asfaque", role: "MERN Developer" });
});
app.post("/api/contact", (req, res) => {
const { name, message } = req.body;
res.json({ success: true, reply: `Thanks ${name}, we received your message!` });
});
Now your server can send and receive JSON data π¦
πΎ Step 7. Connect to a Database (Optional)
You can connect your server to a database like MongoDB to store data.
Install mongoose
npm install mongoose
Connect in your server.js
const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost:27017/myDB")
.then(() => console.log("β
MongoDB connected"))
.catch(err => console.log(err));
Now your server can read/write real data from MongoDB.
π Step 8. Use Nodemon for Auto-Reload
During development, install nodemon so your server restarts automatically whenever you make changes.
npm install -g nodemon
Run your server like:
nodemon server.js
π Step 9. Connect React App with Your Server
If you already have a React app, you can make API calls to your server using fetch or axios.
Example in React
useEffect(() => {
fetch("http://localhost:5000/api/user")
.then(res => res.json())
.then(data => console.log(data));
}, []);
Output
{ "name": "Asfaque", "role": "MERN Developer" }
Boom π₯ β your React frontend is now talking to your Node.js backend!
π Step 10. (Optional) Deploy Your Server Online
You can host your Node.js server using:
Railway / Cyclic / Heroku
Once deployed, your API will be live at a real URL like:
> https://yourappname.onrender.com/api/user
π§ Summary
Step What You Did
1οΈβ£ Installed Node.js
2οΈβ£ Created project folder
3οΈβ£ Installed Express
4οΈβ£ Built a simple server
5οΈβ£ Tested it locally
6οΈβ£ Added routes
7οΈβ£ (Optional) Connected database
8οΈβ£ Used nodemon for convenience
9οΈβ£ Connected with React
π Deployed online π
π Final Thoughts
Youβve just built your own server from scratch! π₯³
Now you can:
Build APIs for your React app
Store and serve data dynamically
Handle routes, authentication, uploads, and more
This is the foundation of MERN stack development πͺ