AppDividend
Latest Code Tutorials

How To build Nodejs Web Server From Scratch

561

Get real time updates directly on you device, subscribe now.

How to build Nodejs web server from scratch. 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 a JavaScript to be used 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” module, which helps in processing server related requests in the web server space.

Installing Node.js server.

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

brew install node

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

node -v
npm -v

Build nodejs web server.

As an editor, I am using Visual Studio Code, and it has excellent support on Node.js as a platform. 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 after, we have used its method createServer. It takes an argument as a req and res. When the browser hit 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 the http://localhost:3000 You can see that we got a response in plain text that says hello world. Great!! We have built the very basic 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": [
    "Node.js",
    "Web",
    "server"
  ],
  "author": "Krunal Lathiya",
  "license": "ISC"
}

Okay, 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 the main purpose is just 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 hit 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.

Related Posts
1 of 10

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.

Leave A Reply

Your email address will not be published.

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