Member-only story

Node JS HTTP server example

Bipsmedium
2 min readNov 17, 2022

--

Hi friends, in this tutorial, you will learn how to create a Node JS HTTP server. Before getting started with the HTTP server creation, you must know that HTTP is a built-in module in node js, and a module in node js acts like a javascript function or library. By using this HTTP module, we can transfer the data over the web browser.

To include a module in node js, we can do so with the help of require() as shown below

var http = require('http');

whereas

  • http inside require is the name of the module and
  • http with var is the object reference variable which will be used later to create the server object or any other object etc.

Create a node js HTTP server object

When you create a node js HTTP server object then the node js file acts like a web server and listens to the server port such as 8080 for localhost etc and gives the response to the browser or client.

Below is an example:- (test.js)

var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/html'}); res.end('Hi this is my first program in node js'); }).listen(8080);

--

--

Bipsmedium
Bipsmedium

Written by Bipsmedium

Hi, This is Biplab and I am a PHP laravel developer and other open source technologies. I am here to share my experience with the community.

No responses yet