Node JS – Part 3

Hi Friends,

In today’s discussion we will create Node JS application in visual studio. Now, let’s create a new project. This time I’ll choose blank Node.JS web Application as shown below in the screen shot.

Now, once the project is created, you can see that there is one file with the name server.js. Now, Server.JS is basically startup JS file for Node Package. Now, let me delete everything written over here, so that we can proceed step by step.Now, the 1st thing which i am going to use here is HTTP Library. This is basically used to handle the HTTP request and response. So, here we’ll use require and ask for HTTP. Now, here i’ll be getting pretty mature intellisense out of the box due to VS and Resharper combination.

Below, you can see in the screenshot i have created http object. Now here you can see in the visual studio editor intellisense support for node as well which is pretty helpful. Once, HTTP object is composed,next job is to create the webserver. So, here we can do the same by using http and then provide a callback function. Now, this function is going to be straight forward, it is going to accept two parameters. 1st one request and the other one is response variable.Then, we have created server and one custom port particular to application.

Now, above in the screen shot you can see that, for running the application i simply clicked F5 in Visual Studio and then, it opened one console window. Then, i have opened one browser chrome in this case and issued one http request from the browser, next same thing is parsed in my script which intellisense is also showing and then i have returned the same output in the browser using the response write function. In this function, i simply composed one HTML body with req object in it. Now, whatever is getting printed in the browser, same thing also get echoed in the console window.

Now, let’s consider the express framework. Now, in Visual Studio project, you might have noticed that there is one option for Node Package Manager(NPM). Basically, Node Package Manager is more like Nuget Package Manager for resolving dependencies or downloading the package and integrating the same with the project. Below, in the screen shot, for downloading the express, i have used NPM.

Now, at this point i can go ahead and verify my dependencies in the package.json file as shown below in the screen shot.

Now, we can install all these packages via command line as well. For that we just need to issue the command npm install package name –save. Now, this –save will save the dependencies in the package.json file. Now, since we have installed express, so in order to use the same in our project we need to include it, and for that we’ll use require statement like we have used for HTTP as shown in the below screen shot. Once we have reference express dependency in our project, it can be executed as function and return one plain and simple object.

Also, in case of express object, we can compose any HTTP verb here like get,post,put,delete …. as shown below in the screen shot. Here, I have composed get with the root of the page(“/”)
Now, in case of this express object, we actually don’t need the callback function, it is taken care by this express object. so, now we can pass the reviews while creating the server object.

Now, as you can see in the above screen shot, that i have again composed one html object while sending the response which is kind of tedious thing doing all the times. So, in the next section in order to get rid of this, we’ll see one more concept say view engine which will take care of all HTML presentations. Till then stay tuned and Happy coding.

Thanks,
Rahul Sahay
Happy Coding

1,861 total views, 1 views today

2 thoughts on “Node JS – Part 3

  1. Nice article. I am following your Nodejs series and it looks very simple and easy to start with Nodejs. In this article I am not able to see the any of images. I need to visit GitHub to download the images.
    Can you please fix the same?
    Gaurav Kumar Arora recently posted…Explaining Less.jsMy Profile

    • Hi Gaurav,

      Looks like that time blog might have not loaded properly. Because all the images are coming properly. Try loading the same again.

      Thanks,
      Rahul

Comments are closed.