In node.js both client program and server program are written in Javascript. In order to create server you can use http module of node.js. We will discuss with a simple program how client server program is done in node.js.

First we have to setup the server.

Node.js provides an httpmodule

This http module is used to create an HTTP server.

this HTTP server which listens at port.

Create a file named index.js in node.js

var http = require(‘http’);

http.createServer(function (req, res){

res.write(‘WELCOME TO MY FIRST PROGRAM!’);

res.end(); //end the response

}).listen(8080);

Set HTTP Header in Node.js

var http = require(‘http’);

http.createServer(function (req, res) {

res.writeHead(200, {‘Content-Type’: ‘text/html’}); // On this line we set up http header in Node.js

In this post we will discuss about the simple program in node.js , in order to understand the basics of node.js programming. We will understand this with the help of an example in node.js. We will go step by step. First in order to execute the node.js program you have to install node.js. After that follow the below steps:

Open a new folder

npm init

First step to create package.json file

It will be created in the new folder.

Create a new file index.js :

In this example we will use http to create server and then client will access it using browser using node.js.

const http = require(‘http’); // http module that is used to create server