Hello World example code with Nodejs

Before starting to learn to write “Hello, World!” program using Nodejs, let’s understand what nodejs is and what are the components ?

1) Modules (or simple node_modules)
As the name itself says a lot, they are bunch of predefined code or libraries which can be imported into another program, We usually have to require them to load into the script e.g. `var somModule = require(‘moduleName’)`
2) Data Types
As we know that Nodejs is purely built on the top of the chrome’s v8 engine (which is javascript), all kind of datatype which is supported by javascript is supported in Nodejs e.g. Array, Object, Number, String, Date etc.
3) Global variables/modules
There are few (in fact many) variables which can be directly be used without importing them, which includes process, JSON, console, module and many more.

Lets start with creating a web server using native http module and run a “Hello world” program.

Step 1. Importing required module

To create simple web server `http` module needs to be required by the application, to import the module write following lines to your script file.

var http = require(‘http’)

Step 2. Create request handler function and bind it to the server, to do the same write code like bellow:

function handler(request, response) {
console.log('Request received: ' + request.url);
// process request here
response.end('Hello world!')
}

http.createServer(handler).listen(8080);

above code will create http server and listen for all request made on itself. Note that hwe have only created a server which will give response “Hello world” to all requests, to give separate responses we will have to create separate functions/handlers.

Step 3. Lets create a JSON response for a request.

function handler(request, response) {
var data = {
message: "Hello world!"
};

response.writeHead(200, {‘Content-Type’: ‘application/json’});

response.end(JSON.stringify(data));
}

Step 4. Bundle up everything into one file.

Filename: app.js

var http = require('http')
function handler(request, response) {
var data = {
message: "Hello world!"
};

response.writeHead(200, {‘Content-Type’: ‘application/json’});

response.end(JSON.stringify(data));
}
http.createServer(handler).listen(8080);

Now execute the following command to run the script from command line interface.

> node app.js

Congratulation! you now have just created a web service with JSON output. Open http://localhost:8080/ inyou browser and you will be able to see the response. Thank you for drawing you attention for reading this guide. We shall come back with another post about more on Nodejs.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.