⚛️ 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. 1️⃣ Installed Node.js

  2. 2️⃣ Created project folder

  3. 3️⃣ Installed Express

  4. 4️⃣ Built a simple server

  5. 5️⃣ Tested it locally

  6. 6️⃣ Added routes

  7. 7️⃣ (Optional) Connected database

  8. 8️⃣ Used nodemon for convenience

  9. 9️⃣ Connected with React

  10. 🔟 Deployed online 🚀

🏁 Final Thoughts

You’ve just built your own server from scratch! 🥳

Now you can: