what is Node js

Node js is open source, server-side javascript language which built on the top of google chrome v8 javascript engine. it uses event-driven, nonblocking IO model that makes it’s more lightweight and efficient and as unlike Apache is multi-thread HTTP-server it means every HTTP request to Apache server, it creates a separate thread and in the case of node js, it handles all HTTP request in single thread asynchronously.

Local Host HTTP Server

Before reading this you need to know basics of  JavaScript

Nod js has built-in HTTP  module (module is same as library) to including that we use require method

var http = require('http');

HTTP module can create HTTP server which will listen for connection (request from the client) on given port and send a response  back to the client.

var http = require('http');

//create a server object:
http.createServer(function (req, res) {
  res.write('Hello World!'); //write a response to the client
  res.end(); //end the response
}).listen(8080); //the server object listens on port 8080

This image will clear your all doubt

so the above code does same as shown in the figure.

In manner to run the node app (HTTP server)

node index.js

index.js is your file name, you can replace with your file name and your local machine will listen for HTTP connection.


Android Developer Intern


Leave a Reply