Final Introduction About Node.js

Hello everybody, how are you guys? Today I’m going to share the last article about Node.js. For those who still have not seen the last articles, I wrote the first article and the second article also about Node. In the last article we created a “pure” web server  in Node. Today I’m going to use a framework to facilitate the creation of a web server. But before that, I’m going to explain what NPM is and what it's used for.

When you install Node,  the NPM, or Node Package Manager, is also installed. The NPM is a big friend to use packages of others developers or enterprises. For those who are as used to with .Net as I am, NPM has the same function of Nuget. In case you still need a better explanation you can access this link to understand more. Ok, NPM is a package manager, but why do you need to learn it now? Because with the NPM will be installed a framework for this article.

The framework that we are going to use is called ExpressJs. It is a framework that facilitates web development by having many things already ready. The link shows your site and there is good documentation to understand better.

First of all, I’m going to create a folder that will be the application folder, called AppExpress.  I go into it to use Node.js Command and inform: “npm init“. This serves to initialize the configuration of an application. Through this command we create a file called package.json that is where all the packages that we use are stored, in other words, all the dependencies that your application requires. When you inform this command, the NPM asks some things, such as application name, version and main file. You are required to provide only the application name. Here our main file is called app.js. The next script, “npm install express –save”, it is used to install the ExpressJs through of the NPM and store the dependence in the package.json file through the  command “–save”. Without this command the express is installed, but is not saved as a dependency and in case you change the machine, the NPM will not know that must install this dependency to the project. With everything ready, now we can start the program. For this, I created the code.
 
The script “node app.js” runs the application in the port 3000.

According to the code, we started the application creating an instance of ExpressJs and in next line was created the app variable. After, a route in the root that shows a “Hello World” was created. If you wish you can create many routes. For creating a route, we have created a function that receives two parameters, request and response. Through response variable, we can use the Send function that shows the text configured. Making the server be listened to in the port 3000 is easy, you must use the “listen” function and like this we will have our application ready.

This application is very basic. For this, we will be improving. I go to use the NPM again to make a new installation. I want a new application and so I create a new folder and this folder informs the command: “npm install express-generator –g”. In this installation I install the express-generator that is a tool of Express JS to generate a scaffold, I mean, creating an application already ready.


 

Generating the scaffold with express-generator is a piece of cake, you must use the command “express appName”, where appName is the folder name that you are creating together with the app generated. But you can also use some options according the image above. But before that a note, when you use the express-generator  all files needed for the project are created, including the project.json,


 

As all framework web is used a engine of visualization to convert specific codes in HTML, equal we have the Razor to MVC .Net. In ExpressJs we have some options of engine as Jade, EJS, Handlebars and Hogan. The engine Jade is very good and decreases the typing of code to generate HTML and I'm going to use that here. After informing the command above, the structure above is created to the application. Understanding the files generated is not tough. I'll explain some files and folders:
  • App.js is the main file of application, here we have the configurations.
  • In public folder is where the client files stay.
  • In routes folder we have the routes of system.
  • In views folder we have our views that are generated through of Jade engine.
  • File bin/www is the initialization of application. It is here that we must start.

For initializing the app you must type “npm start”. Note that in all the previous samples we used the command “node file.js” and now we are using “npm start“. If we open the file package.json, you will see a command called “start” that has the script “node ./bin/www“, what do I mean? Typing “npm start” is the same as “node file.js”. In others word, this is just a shortcut. You can make however many commands you wish.

If we access the www file, you will see the port 3000 configured.

Now we are going to see the app generated by the express-generator and for this I’m going to show the app.js file. The  first six linesof  this file is creation of variables that receive an instance. Below I explain the responsibility of each variable:

  • Express: Instance of framework.
  • Path: Utility for manipulation and transformation of path of file.
  • Favicon: Configuration of the icon that is shown in the browser.
  • Logger: App log in requests HTTP.
  • CookieParser: Analyser of cookie.
  • BodyParser: Analyser of body of requests.

Cookie-Parser, Body-Parser and Logger are middlewares. In this article I’m not going to explain what middlewares are. Cookie-Parser fills the cookies of object request, request.cookies. Body-Parser fills the body of object request, request.body. This properties are filled for these middlewares, but those in Node are created in run time by the middlewares before that the request arrives in the actions.

Now that you  understand for  each variable of instance created in app.js file, we can see that they are used after to configure the application and like this enable the functions. By default, only the ExpressJS does not have anything configured and it is your responsibility to enable what you want, for this we use the express-generator to create something already ready.

The Express Js is a good framework and it is the more used in the Node world. It is easy to create an application and with the express-generator you can create a application in minutes. But everything only can be possible if you use the NPM as package manager and remember that each installation must use the package.json file.

Node is an excellent tool of development and today is very successful. Many companies are leaving your framework and changing to Node. Here I have finished the three articles about Node.js and hope that you have understood about the subject. 

Read more articles on Node.js: