Build a Simple Node.js Web Server for Beginners

In this article, I’ll show you how to build a Nodejs web server from scratch. It’s very easy to setup. Let’s follow these steps:

Table of Contents

  1. Download and Install Node.js
  2. Create a Project
  3. Run the Server
  4. Install a Framework

Step 1 : Download and Install Node.js

Very first, we need to install Node.js on our computer. Download Node.js from here. I’m using Windows 10 64-bit. After downloading Node.js, install it.

Step 2 : Create a Project

Create your first project folder in any drive. Then create a file called ‘server.js’ inside the project folder. I’m creating the project folder in the xampp’s htdocs folder called ‘nodejs’. The path is E:\Xampp\htdocs\nodejs. In this folder, I’ve created a file called ‘server.js’.

Open the server.js file and paste this code:

var http = require('http');

var server = http.createServer(function(req, res) {
    res.writeHead(200, { "Content-type": "text/plain" });
    res.end("Hello world");

server.listen(4000, function() {
    console.log('Server is running at 4000')

Step 3 : Run the Server

To run the server, navigate to the project folder via CMD and run this command:

node server.js

Our server is running at 4000 port. Now open the URL in a browser http://localhost:4000/. If you follow the above steps correctly, you will see “Hello world” message.

We have successfully created a simple nodejs server. Now we will install a Nodejs framework called ‘express js ‘. Like other frameworks, Nodejs framework provides various functionalities that can be very helpful.

Step 4 : Install a Framework

We have to create package.json file. In the CMD, type CTRL+C to close the server. Then type this in the CMD:

npm init

It’ll ask for some information from you. After providing the details, you will see package.json file like this:

  "name": "nodejs-server",
  "version": "1.0.0",
  "description": " Create a simple node.js server",
  "main": "node server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  "author": "Md. Obydullah",
  "license": "ISC"

Now, Let’s install the framework by hitting this command:

npm install express --save

You will see a folder called ‘node_modules’ and a file called ‘package-lock.json’ in your project directory. It’ll also update the ‘package.json’ file.

Now clear server.js file and paste this code:

var express = require('express');

var app = express();

var port = 4000;

app.get('/', function(req, res) {
    res.status(200).send('Hello world');

app.listen(port, function() {
    console.log('Server is running on port:',port);

We have installed the framework. Now run the server by typing this command:

npm start

The server is started at port 4000. Now you can see the same ‘Hello world’ message at http://localhost:4000/.

So, at first, we run the server without the framework. Secondly, we run the server with the framework.

I hope now you can easily build a Node.js server.

Software Engineer | Ethical Hacker & Cybersecurity...

Md Obydullah is a software engineer and full stack developer specialist at Laravel, Django, Vue.js, Node.js, Android, Linux Server, and Ethichal Hacking.