Node.js for beginners 2 - Our first application

Thursday, November 19, 2015

Rather than going from step by step, we will go ahead and write our first application right away and execute it to have some idea about what we are going to do.






Step 1 - Create a new JavaScript file, named firstapp.js with the following content in it.
var http = require("http");

http.createServer(function (request, response) {
   response.writeHead(200, {'Content-Type': 'text/plain'});
   response.end('Greetings! This is my first node application');
}).listen(9000);

console.log('Server is running at http://127.0.0.1:9000/');

Step 2 - Start the server we just wrote using the following command
$ node firstapp.js
and you will see the following message on your terminal window on success.
$ Server is running at http://127.0.0.1:9000/

Then open any web browser and navigate to http://127.0.0.1:9000/ and you will see our greeting message.


That's it. Now let's dive deep into the code we wrote and understand what happens at each important step.

Explanation of our first application

1. Node modules

We start our code by importing the http module.
var http = require("http");
This module has the required code for starting a node server. When we import this module into our source code, we can simply call any function exported in the module.
In short, at this level, it is sufficient to understand that there are things called "modules" that have JavaScript functions which we can call from another JavaScript file by first importing them.
I will explain about modules in detail in the next tutorial.

2. Creating a server

As explained, we can now call the respective function to create a node server instance.
http.createServer(/*func param*/);
This function takes another function with request and response arguments. That is, on execution of the createServer() function, it will also execute the function given as the argument.

In this tutorial, we have passed the following function as the argument of the createServer() function.
function (request, response) {/*function body to process request and response*/}

Arguments of this function are an http request and a response respectively. We can play with those in whatever ways without violating the basic rules of JavaScript.

3. Listening on a port

With the previous steps, the createServer() function now looks like this,

http.createServer(function (request, response) {
   /*Some code to process the request*/
})
However this code is not yet complete. If you run this node application, you will see that the execution completes just after you start it. That is, if you want to make this server instance to be up and running, you have to keep listening at an arbitrary port. For this, you can call the listen(/*port number*/) method associated with the server instance.

http.createServer(function (request, response) {
   /*Some code to process the request*/
}).listen(9000);

Now our application will stay up, listening on the given port for requests, until the server is shut down. With this we can mark the end of creating our first node application. You can play with this code, by having custom code(eg- 'console.log()') inside the argument function, or inside the optional callback function available with the listen() method.

After you have played enough, you can proceed with our 3rd tutorial !

Link

1 comment:

  1. This is a very good tutorial. Everything is explained well. Publish more tutorials !

    ReplyDelete