Node.js  

🌐 How to Make a Server (with Node.js + Express)

βš›οΈ 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)

  • βœ… It’s fast and lightweight

  • βœ… It integrates easily with databases like MongoDB or MySQL

🧩 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:

  • Render (free & easy): https://render.com

  • Vercel (for serverless): https://vercel.com

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:

  • 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 πŸ’ͺ