AllNode JS

Your First NodeJs Application

If you are looking for a “Hello World!” example, I am sorry, you have landed to a wrong page, for hello World, your best bet is the Nodejs site.
Lets just have a brief discussion on what Node (Developers kinda like this name than Nodejs) really is. Node is a platform based on Google’s high performance (& open source) V8 Java script engine, if you are a “Geek” you must have read about it as Chrome uses it as well. Node being build on the Javascript engine, provides a JS like environment for Server side development. Yes, you read this right. Node is Server side Javascript framework.

Ok! So lets quickly move to creating our simple Node App, what it does is as usual would listen to a port as server and would be able to read your request parameters. šŸ™‚
First, we need to create a server –

var http = require("http");
function onRequest(request, response) {
response.writeHead(200, {"Content-Type": "text/html"});
response.write("Hello World!");
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");

You got me this time, it starts with a Hello World but is not mere Hello World. Anyways, lets break it down –
First we need to include http library to enable Node respond to HTTP requests and responses. Its similar to any server side scripting language you can think of.
Now here is the interesting part .. http.createServer(onRequest).listen(8888);
This line creates a server which listens on port 8888(you can have any legal value for port) and has an argument onRequest. Wait, is the argument a function ? Yes! It is, if you come from Java / .NET World(like me), it sounds bit weird, but if you have some Javascript exposure(like me) you can understand its no Rocker science.
console.log simply prints its arguments to the standard output stream, you command prompt in this case.
Now our onReuqest function is currently a dumb function, it just sets content type to text/html and sends a response Hello World and HTTP response code 200 (Success).
You can always run your Node program via the command line (I have saved the file as hello.js) –

node hello.js

So, lets spice things little bit, by adding capability to know what URL is being requested and any request parameter associated with it.
So to read the URL you need to add the below lines to your code

var url = require("url");
var _url = url.parse(request.url, true);
var pathname = _url.pathname;

That’s it. Import url library, call parse function with request.url as an argument, and you can directly get the path. You must have observed, true being passed as second argument to the parse function. The second argument asks node to parse the query / request parameters as well, the default being false. If you use false, or provide only one argument Node will only parse URL for you.
So, lets get back to work and see if we can read request params as well, since Node has already parsed it for us.
Use any of your fav. editor(I love Notepad++), create hello.js and place it inside your Node installation folder –

var http = require("http");
var url = require("url");
function onRequest(request, response) {
var _url = url.parse(request.url, false);
var pathname = _url.pathname;
console.log("Request for " + pathname + " received.");
var content = "Howdy!!<br> Have you requested for the page "+pathname ;
if(_url.query)
{
content += "<br><br>Ohh! You have also provided me below data - <ul>";
for(i in _url.query)
{
content += "<li>" + i + " = " + _url.query[i]Ā  +"</li>";
}
content += "</ul>";
}
response.writeHead(200, {"Content-Type": "text/html"});
response.write(content);
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");

You can read any request parameter using _url.query[‘varName’]. That’s it.
Start you server

node hello.js

And play with the wonderful Java script lang. šŸ˜‰

Please leave a comment in case you have any questions. šŸ™‚

5 thoughts on “Your First NodeJs Application

  1. Can I just say what a relief to find someone who actually knows what theyre talking about on the internet. You definitely know how to bring an issue to light and make it important. More people need to read this and understand this side of the story. I cant believe you’re not more popular because you definitely have the gift.

  2. I simply want to mention I am just very new to weblog and absolutely loved this web site. Almost certainly Iā€™m likely to bookmark your website . You surely come with superb posts. Kudos for sharing with us your blog.

Leave a Reply

Your email address will not be published. Required fields are marked *