How to Create a Simple Web Server with Node.js

coder_nandancoder_nandan
4 min read

Creating server with Node.js

Node.js is a powerful JavaScript runtime environment that allows developers to create scalable and efficient web servers. In this guide, we'll walk through the steps to create a simple web server using Node.js.

Step 1: Create a New Project

Create a new directory for your project and navigate to it in your terminal/command prompt.

mkdir my-web-server
cd my-web-server

Step 2: Create a Server File

Create a new file called server.js and add the following code:


const http = require('http');

const server = http.createServer((req, res) => {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
});

server.listen(8080, () => {
  console.log('Server running on port 8080');
});

Step 3: Require HTTP Module

The http module is built into Node.js and provides functionality for creating a web server.

const http = require('http');

Step 4: Create Server

Create a new server instance using the createServer method.

const server = http.createServer((req, res) => {  });

Step 5: Define Request Handler

Define a callback function to handle incoming requests.

(req, res) => {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}

Step 6: Listen on Port

Specify the port number for your server to listen on.

server.listen(3000, () => {
console.log("Running")l
});

Step 7: Start Server

Run your server using Node.js.

node server.js

Result

Image description

What is Express.js?

Express.js is a framework for Node.js to provide developers with robust tools for building a back-end for a website or web application. It includes routing, simple setup for templating, and many more benefits. Because of it’s maturity and ease of use, Express.js has been the most popular Node.js framework for years. There are even other Node.js frameworks built on top of it, such as Sails.js, Koa.js, and Nest.js.

Despite the number of tools it provides, it is a very unopinionated framework. The ability to structure your back-end any way you want means that it can be as flexible as you need it to be. For this reason, it is a staple in many developer’s toolkits. Express.js gives us the “E” in the MEAN stack, MERN stack, or MEVN stack. Now that you have a better understanding about what Express.js is, let’s build something with it!

# Terminal
$ npm i express

The next step to perform is setting up your git repository. This is an optional step depending on whether you are using git for version control. If you are using git, I also recommend adding a .gitginore file to exclude the /node_modules directory from the repo. Keeping the node_modules directory will bloat your codebase and will cause problems with git. The code for the .gitignore is below:

#.gitignore
node_modules

Creating the Express server

The first file you have to create in your express project is going to be a server file. The purpose of this file is to set up any middleware you’ll be using, configure a templating engine, create the server itself, and more. You could also put your routes in this file, but in my opinion that gets pretty messy if you have any more than 1 or 2 routes. You can call the server file whatever you want, although it is often called app.js or server.js. I will be calling mine server.js in this project.

# Terminal
$ touch server.js

Open your newly created file and you can begin building a simple server. At first, we will import express and designate a port on which our server can run. Inside your server file, add the following code:

// server.js
const express = require("express");
const app = express();
const port = 4000;

app.listen(port, () => {
  console.log(`Success! Your application is running on port ${port}.`);
});

Let’s take a closer look at what’s going on here. Line 1 imports the express library into the project. Line 2 creates instantiates express inside a variable called app. I am creating a variable for my port on line 3 so I can change it in one place and have it updated anywhere I’m logging or using it.

Once those variables are set, you can create the server by using app.listen(). First, pass in the port at which you want the server to run. This can be any value as long as it’s an integer. After the port, can provide a callback. In this case, I’ve used the callback to log a message to the console indicating the server is running. This isn’t required, but I like having the message to indicate that the terminal is working as expected.

Result:

Image result for how to create a simple web server with express js

Conclusion

What I initially thought would be a complex task turned out to be a simple, logical series of steps. Node.js makes it incredibly easy to create a basic web server with minimal setup. With just a few lines of code, I have a working server that listens to requests and sends responses.

This simple setup lays the foundation for more advanced web development—handling routes, serving HTML, connecting to databases, and more. But every powerful application starts somewhere, and this is where it begins.

0
Subscribe to my newsletter

Read articles from coder_nandan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

coder_nandan
coder_nandan

I'm a full-stack developer who loves turning ideas into real, usable products. I work with React, Node.js, MongoDB, and Tailwind to build clean, scalable web apps. I don’t just build demo projects — I focus on solving real problems with code. Always learning, always shipping.