In this we will create hello world by two ways :

  1.  Simple by console.log.
  2. By creating actual server.

Install node.js

$ sudo apt-get install nodejs

Simple Program :

Create file say  ” app.js “. Inside app.js add following code

console.log("Hello, Worldn");

Output : 

Hello, World

Open terminal and compile

$ nodejs app.js

Creating Actual Server :

// Create http ( this is in build in node )
var http = require('http');
// Create server 
var server = http.createServer(function (require, response) {
    // Write head with content-type plain
    response.writeHead(200, {"Content-Type" : "text/plain"});
    response.end("Hello, Worldn");
});

// Mention port whether server should listen to request
server.listen(8000);

console.log("http://localhost:8000");

Open terminal and compile

$ nodejs app.js

Open browser and write

http://localhost:8000
Advertisements