Familiarize yourself with the Express Framework in Node.js


In the previous articles, readers read with Taimienphi.vn to learn about REPL Terminal in Node.js, and how to create applications in Node.js? In the next article below, Taimienphi.vn learn and familiarize with Express Framework in Node.js.


The Express Framework in Node.js is built in such a way that it works within the Node.js web application framework, providing a powerful set of features for building single-page, multi-page web applications as well as web applications. hybrid.

Familiarize yourself with the Express Framework in Node.js

Refer to the following article of Taimienphi.vn to learn and become familiar with Express Framework in Node.js.

Table of Contents:
1. What is Express?
2. Install and use Express
3. What is a route?
4. Create Web Server Template with Express.js

1. What is Express.js?

Express.js is a Node.js web application server framework, specifically designed to build single-page, multi-page web applications as well as hybrid web applications, and become the standard framework for Node. js.

Express is the backend part of the MEAN compartment. MEAN is a free and open source JavaScript software used to build dynamic websites and web applications containing the following components:

1. MongoDB – Standard NoSQL database
2. Express.js – Default web application framework.
3. Angular.js – Framework JavaScript MVC used in web applications.
4. Node.js – Framework used in scalable network and server applications.

The Express.js framework makes it easier for application development to handle many types of requests such as GET, PUT and POST and DELETE requests.

2. Install and use Express

Express is installed via Node Package manager. By opening the command line utility and enter the following command:

npm install express

The above command will ask the Node package manager to download and install the express modules.

Next, use the newly installed Framework Express and create a simple Hello Word application:

The application will create a simple server module, listen on port 3000. In this example if a request is made via a web browser on this port, the server application will send a ‘Hello’ World ‘response to client.

Familiarize yourself with the express framework in node js 2

Familiarize yourself with the express framework in node js 3

In the code above:

1. The first line uses the require function to include the express module.
2. Before starting to use express module, we need to create an object of express module.
3. Here we will create a callback function. This function will be called at any time when the user browses the root directory of the created web application, which is http: // localhost: 3000 . Callback function will be used to send Hello World string to the website.
4. In the callback function, the Hello World string is sent back to the client. The “res” parameter is used to send content back to the site.
5. Use the listen function to let the server application listen for client requests on port number 3000. Or you can specify any number of available ports.

If the above command is successfully executed, the output will look like the following when you run the code on the browser:

Familiarize yourself with the express framework in node js 4

From the output:

– It can be clearly seen if we browse to the URL of localhost on port 3000, the string Hello World will display on the page.

– In this code, we specifically mention the server to listen on port 3000, so we can see the output when browsing to this URL.

3. What is a route?

Route refers to determining how an application responds to a client’s request to a specific endpoint.

For example, the client may make http GET, POST, PUT or DELETE requests for different URLs, such as one of the URLs shown below:

http: // localhost: 3000 / book

http: // localhost: 3000 / sinhvien

In the example above:

– If the GET request is made to the first URL, the response will be a list of different book types.

– If the GET request is made to the 2nd URL, the response will be a student list.

– Based on the URL to be accessed, another function on the web server will be called and the response (response) will be sent to the client. And this is called the concept of a route.

Each route can have one or more handler functions, executed when the route is matched.

Following is the general syntax for a route:

app.METHOD (PATH, HANDLER)

Inside:

– app is a variant of the express module.

– METHOD is an HTTP request method (GET, POST, PUT or DELETE).

– PATH is the path on the server.

– HANDLE is a function that is executed when the route is matched.

Refer to the example below to better understand the route. In this example we will create 3 routes:

1. Route A / Node displays the string “huong dan Node” if the route is accessed.

2. The A / Angular route displays the “huong dan Angular” string if this route is accessed.

3. A default route will display the string “welcome to Taimienphi.vn”.

Basically the code that Taimienphi.vn uses will still be the same as the code in the previous examples. The code below is an add-on to show how the route is deployed:

var express = require (‘express’);

var app = express ();

app.route (‘/ Node’, get (function (req, res)

{

res.send (“huong dan Node”);

});

post (function (req, res)

{

res.send (“huong dan Angular”);

});

put (function (req, res)

{

res.send (‘welcome to Taimienphi.vn’);

}));

In the code above:

1. Here we define a route if URL http: // localhost: 3000 / Node is selected on the browser. For routers, we attach the callback function, which is called when we browse to the URL node.

This function has 2 parameters:

– The main parameter we use is the “res” parameter, used to send information to the client.

– The parameter “req” has information about the requests being made. Sometimes additional parameters may be sent as part of the request being made, so the “req” parameter can be used to find additional parameters to be sent.

Use the send function to send the “huong dan node” string back to the client if the Node route is selected.

3. Here we define a route if URL http: // localhost: 3000 / Angular is selected on the browser. For the route, we attach the callback function, which is called when browsing to the Angular URL.

Use the send function to send the “huong dan Angular” string back to the client if the Angular route is selected.

5. This is the default route selected when the user browses to the application’s route – http: // localhost: 3000 . When the default route is selected, the message “welcome to Taimienphi.vn” will be sent to the client.

The output will look like the following:

acquainted with express framework in node js 5

From the output:

– If you browse to the URL of localhost on port 3000, you will see the string “chao mung Taimienphi.vn” displayed on the page.

– In the code above we mention the default URL that will display in this message.

– If the URL changes to / Node, the corresponding Node route will be selected and the “huong dan Node” string will be displayed.

– If the URL is changed to / Angular, the corresponding Node route will be selected and the “huong dan Angular” string will be displayed.

4. Create Web Server Template with Express.js

From the example above, you can see how we decide which output to display based on the route. This type of route is used in most modern web applications. Another part of the web server uses templates in Node js.

If you want to create Node applications quickly, the simplest and easiest way is to use application patterns. In the example below, Taimienphi.vn will take an example of the jade framework for the template.

Install Jade via the Node Package manager. To do this, enter the following command in the command line window:

npm install jade

The above command will ask the Node package manager to download and install the Jade module.

Note: The latest version of jade node is not accepted, instead we use pug.

Use the newly installed jade framework and create some basic templates:

First create a jad template. Create a file named index.jade and insert the code below:

Familiarize yourself with the express framework in node js 6

In the code above:

1. Here we specify the title of the page to be changed to any value when this template is called.

2. The text in the tag header will be replaced to any value via the jade template.

Familiarize yourself with the express framework in node js 7

var express = require (‘express’);

var app = express ();

app.set (‘view engine’, ‘jade’);

app.get (‘/’, function (req, res)

{

res.render (‘index’,

{title: ‘Guru99’, message: ‘Welcome’})

});

var server = app.listen (3000, function () {});

Explain the code above:

1. First to specify the application is the “view engine” that will be used to display the template. So we will use jade to display the templates.

2. The render function is used to display a web page. In this example, the template (index.jade) is created.

3. Convert the Guru99 and Welcome values ​​into the corresponding header and message parameters. These values ​​will be replaced with the “header” and “message” parameter values ​​declared in the index.jade template.

If the above command is successfully executed, the output will be displayed when you run the code on your web browser.

Familiarize yourself with the express framework in node js 8

From the output:

– We can see the title of the page is set to Guru99 and the header is set to Welcome.

– This is because the jade template is called in the Node js application.

So above you read Taimienphi.vn just to learn and familiarize with the Express Framework in Node.js. Basically the Express Framework is the most popular framework used in developing Node.js applications, and is integrated on top node.js frameworks, supporting server-based monitoring of applications.

https://thuthuat.taimienphi.vn/lam-quen-voi-express-framework-rong-node-js-43610n.aspx
Route is used to redirect the user to other parts of the web application based on the request made. The response for each route may vary, depending on what is displayed to the user. Templates are used to effectively insert content. Jade is one of the most popular template engines used in Node.js applications.

.

Add a Comment

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