Today I’m going to use a framework to facilitate the creation of a web server in Node.js. But before, I’m going to explain what is NPM and for that is used.
When you install Node.js, also is installed the NPM, Node Package Manager. The NPM is a big friend to use packages of others developers or enterprises. For those who are used to with .Net as me, NPM has the same function of Nuget. Case you still need of an explanation better you can access this link to understand more. Ok, NPM is a package manager, but why do I need to learn 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 the web development by have many things already ready. The link shows your site and there is a good documentation to understand better.
First of all, I’m going to create a folder that will be the application folder, called AppExpress. Into it I go to use Node.js Command and inform: “npm init“. This serve to initialize the configuration of a application. Through this command we create a file called package.json that is where are stored all the packages that we use, in other words, all the dependencies that your application requires. When you inform this command, the NPM makes some asks, as application name, version and main file. You are required to inform 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 of command “–save”. Without this command the express is installed, but is not saved as dependence and case you change of machine, the NPM will not know that must install this dependence to the project. With everything ready, now we can start the program. For this, I created the code below:
The script “node app.js” run the application in the port 3000.
According with the code, we started the application creating an instance of ExpressJs and in next line was created the app variable. After, it was created a route in the root that show a “Hello World”. If you wish you can create many routes. For creating a route, we have created a function that receives two parameter, request and response. Through of response variable, we can use the Send function that shows the text configured. Making the server be listened 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 in 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 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 also can use some options according the image above. But before a note, when you use the express-generator is created all files needed to the project, 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 it is it that I’m going to use here.
After informing the command above, the structure above is created to the application. Understanding the files generated is not tough. Even like this I 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 samples previous we used the command “node file.js” and now we are using “npm start“. If 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 that “node file.js”. In others word, this is just a shortcut. You can make how 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 six first lines 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. This article I’m not going to explain what middlewares are, in another article I explain. But I make a resume. 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 are understanding for that serve 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 Express Js does not have nothing configured and it is your responsible 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 easily 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 make much success. Many companies are leaving your framework and changing to Node. Here I finish the three articles about Node.js and hope that you have understood about the subject.