Skip to main content

Command Palette

Search for a command to run...

Creating Your First Server in Node.js – Step-by-Step Guide for Beginners

Published
2 min read
T

I am a Computer Science graduate learning backend development with Node.js. I enjoy writing beginner-friendly articles and sharing what I learn along my journey. Currently focused on JavaScript, Node.js, and building real-world projects.

If you’re starting backend development, one of the first milestones is creating a server.

In this blog, we’ll learn how to create a simple Node.js server from scratch, understand how it works, and run it locally.


🔹 What is a Server?

A server is a program that:

  • Listens for client requests

  • Processes them

  • Sends back responses

In Node.js, we can easily create a server using the built-in http module.


🔹 Prerequisites

Before starting, make sure you have:

  • Node.js installed
    👉 Check using:

      node -v
    

🔹 Step 1: Create a Project Folder

mkdir my-first-server
cd my-first-server

Initialise Node.js project:

npm init -y

🔹 Step 2: Create a Server File

Create a file:

index.js

🔹 Step 3: Import the HTTP Module

const http = require("http");

The http A module allows Node.js to create web servers.


🔹 Step 4: Create the Server

const server = http.createServer((req, res) => {
  res.write("Hello, this is my first Node.js server!");
  res.end();
});

What’s happening here?

  • req → request from client

  • res → response sent to client

  • res.write() → sends data

  • res.end() → ends the response


🔹 Step 5: Start the Server

server.listen(3000, () => {
  console.log("Server is running on port 3000");
});

🔹 Step 6: Run the Server

node index.js

Open your browser and visit:

http://localhost:3000

🎉 You’ll see:

Hello, this is my first Node.js server!

🔹 Handling Routes (Basic)

const server = http.createServer((req, res) => {
  if (req.url === "/") {
    res.write("Home Page");
  } else if (req.url === "/about") {
    res.write("About Page");
  } else {
    res.write("Page Not Found");
  }
  res.end();
});

🔹 Why Use Node.js for Server?

✅ Fast & scalable
✅ Non-blocking I/O
✅ Same language for frontend & backend
✅ Huge npm ecosystem


🔹 Common Interview Questions ❓

✔ What is http module?
✔ What does req and res mean?
✔ What is res.end()?
✔ Difference between server and API?


🔹 What’s Next After This?

After creating a basic server, learn:
➡ Express.js
➡ REST APIs
➡ Middleware
➡ Database integration


🔹 Final Thoughts

Creating your first Node.js server is the first real backend achievement.

Once you understand this:

  • Express.js becomes easy

  • APIs make sense

  • Interviews feel confident

Every backend developer starts with one server 🚀


🙌 Thanks for Reading!

If this blog helped you:

  • Like ❤️

  • Share 🔁

  • Comment 💬

More Node.js beginner blogs coming soon 👨‍💻🔥