Create a NodeJs Hello World example

September 26, 2014 | Updated last September, 2015 | 934 views | Comments

Simple and easy Hello World Examples to get started with NodeJs.

 

Console Example (CentOS)

Create a javascript file named hello.js, and copy the following line of code on it.

console.log("NodeJs Hello World Console Example...");

Open a terminal and go to the location where hello.js was saved, and type node hello.js.

# node hello.js

"NodeJs Hello World Console Example..." will be printed on the console.

 

Http with Console Example (CentOS)

This time, create another javascript file named hello-http.js, and copy the following lines of code on it.

var http = require('http');

var server = http.createServer(function (request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.end("NodeJs Hello World Http with Console Example...");
});

server.listen(8000);

console.log("Server running at http://127.0.0.1:8000/");

Open a terminal and go to the location where hello-http.js was saved, and type node hello-http.js.

# node hello-http.js

"NodeJs Hello World Http with Console Example..." will be printed on the console.

Open a browser and go to http://127.0.0.1:8000/, below is what it will display

 

Related Tutorials


Must Read