Go a round with the Express framework

Tram Ho

1. Introduction

When learning NodeJs , you will surely be exposed to many different Node frameworks and surely anyone who has learned or exposed to NodeJS will work with Express framework – a web application framework of NodeJS .

I think this is a framework that is quite easy to learn for beginners. It is quite convenient when it comes to supporting a lot of features on the web or on mobile. They help us build the backend more easily. There are a few features that Express supports such as:

  • Middleware
  • Routing
  • Templating
  • Debugging

Besides, there is a lot of API support.

Sounds interesting already, let’s go to find out.

2. Installation

Finish creating express already. First of all, print Hello World first ✌️

Create an additional file index.js with the content:

Run node index.js to see the results.

3. Routing

As in the simple example above, we have also looked through the fog about the express route by declaring as above.

With the above code we can understand the structure to declare a route according to structure

Inside :

  • app an instance of express
  • METHOD HTTP methods (get, post, delete, put)
  • PATH path in browser.
  • HANDLER a function to execute the code when the path is called.

Route parameters

We can also declare the params for the url with the syntax :param .

Use the Router to prefix the route

If we want before entering any path, we want it to start with /admin example, we just need to declare the following:

In the above code, we have declared a Router object, this is a pretty powerful object of express often used in cases like middleware or route handling. It supports several methods that you can see for more reference here .

app.route ()

Another way to define a route is to use app.route () . Using this method will find the code quite concise and easy to avoid confusion, and also avoid duplication when thinking of multiple routes that share the same paths. Example of not using app.route () .

It can be seen that there are up to 3 segments we all declare as /books/:book , if there are more than 3, it looks quite difficult. A more optimal way of writing is to use app.route () .

4. Middleware

When a request is sent, Express will execute middleware functions in turn until a response is returned to the user. They have access to the request or response .

If a Middleware function finishes but it is not the last function in the function we need to execute, we need to call next () function otherwise the application will hang at that function.

There are five middleware types used in Express :

  • Application-level middleware
  • Router-level middleware
  • Error-handling middleware
  • Built-in middleware
  • Third-party middleware

We’ll go into finding out what each of these types is

Application-level middleware

When we instantiate an Express application we will have an object representing that application, namely the app . This object is usually declared by us as the name app .

Then we consider this app to be at the application level, the highest level. We can declare application-level middleware using app.use () or app.METHOD (where METHOD is HTTP methods).

Declare middleware for a specific path:

If you do not declare a specific path, it will run by default when calling all routes:

Router-level middleware

This middleware is similar to application -level middleware , except that the Router-level middleware is an instance of express.Router () .

To use the route level middlware we will also use router.use () or router.METHOD as the Application-level middleware .

Above, if everyone noticed, in the section about Route I also used router.use () to define the route prefix.

Declare middleware to run at any request:

Error-handling middleware

This is the middleware for error handling. But when using this middleware, it is necessary to declare all 4 parameters err, req, res, next . Although the next parameter is not used, this declaration is required for Express to recognize it as an error handling function.

Suppose when the server fails.

Third-party middleware

Use this middlware so that we add other necessary functionality to the app. Suppose we are adding a middleware called cookie-parser .

Just run the command

Then declare at the application level or route level , it is up to you to define it. For example :

Other middleware can be found here .

5. Use template engines

If you have ever interacted with Ruby on Rails , the access to NodeJS template engines is quite easy. If Rails has erb , NodeJS has ejs , or Rails has slim and Node has pug . I see not too much difference in the use of template engines in Node if you word comes into contact with Rails .

To use the pug template engine just install

and declare

And this declaration is similar if you use any other template engines .

6, Express database

Express can be combined with many different types of databases. Assuming we connect to mysql , we need to install the mysql module first.

and declare to use

To create a database we need to declare some simple configurations such as hostname , database name ..

This is just one example of how to combine Express with database . In fact, I see that people use NodeJS with MongoDB more often. To learn how to use and combine them you can see more at https://github.com/mongodb/node-mongodb-native .


Above is my understanding of the Express framework .

Share the news now

Source : Viblo