How to build Node.js Web Server From Scratch

how to build nodejs web server tutorial

We will create a web server. Node.js is a very lightweight server. Node js is a platform for building fast and scalable server-side applications using JavaScript. Node.js enables JavaScript for server-side scripting and runs scripts server-side to produce dynamic web content before the page is sent back to the web browser. There are many modules, such as the “http” and “request” modules, which helps in processing server-related requests in the webserver space.

Installing Node.js server.

I am using Mac. So we can get the node.js using homebrew. It also installs npm, which is the node package manager.

brew install node

Now, you can check the version of the node.js using the following commands.

node -v
npm -v

How to Build Node.js webserver

To build a node.js server, use the built-in HTTP module. The HTTP module provides a createServer() function that takes a callback function and creates a server. You can also use the express framework to create a server in which you need to use the app.listen() function which will create a server.

As an editor, I am using Visual Studio Code, and it has excellent support on Node.js as a platform. So I  highly recommend VSCode for the development. You can find more details on their website.

Step 1: Create a project folder.

First, create a folder using the following command.

mkdir node-server-tutorial

Okay, now go into that folder and open that folder in your editor, and in my case, it is code.

Inside the root folder, make one file called server.js.

Step 2: Write the server.js file.

// server.js

var http = require('http');

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

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

So what I have done is use the built-in http module. Then, we have used its method createServer. It takes an argument as a req and res. When the browser hits the server, then in response, we will send plain text. Now go to your terminal. I am using iTerm for this project. Type the following command.

Step 3: Run the node.js server.

node server

iterm tutorial

So as the terminal said, our node.js web application is running on port 3000. We should check this in our browser. As a default browser, I am using Google Chrome as it is most famous now. Go to http://localhost:3000. You can see that we got a response in plain text that says hello world. Great!! We have built a fundamental node.js server.

how to build node js web server tutorial

We do not build a server from scratch. There is one framework called express js. We will use express js to construct a server because it is made on top of the node.js framework provides various functionalities that can be very helpful to build a web application.

Step 4: Install the express framework.

Go to your terminal and hit the following command.

npm init

It will create a file like this. Some options may differ as per your project.

  "name": "node-server-tutorial",
  "version": "1.0.0",
  "description": "How to create node.js server",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  "keywords": [
  "author": "Krunal Lathiya",
  "license": "ISC"

Okay, the next step is to install the express framework.

npm install express --save

It will also update the package.json file.  Now, we will see how we can use express to build a web server that’s main purpose is just to respond to a request.

// server.js

var express = require('express');

var app = express();

var PORT = 3000;

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

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

So what I have done is first require the express module in our application and then use the following two methods.

  1. get()
  2. listen()

The server is listening on port 3000. So When a request hits the root(/) URL pattern of the route, then in response, we send the 200 status code plus the content that needs to be sent to the user. Now again, start the server, but now we use a different command.

build express server

Express js gives us routing capabilities out of the box. So we can give any URL pattern that is SEO friendly, and according to that, we can send the response. In this case, we will get the same output.

how to build a node server

Finally, our Build nodejs web server tutorial is over. Thanks for taking it.

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.