Express middleware

Alice Richardson
3 min readNov 18, 2020

--

What is the function of middleware?

Express middleware are functions that execute during the lifecycle of a request to the Express server. Each middleware has access to HTTP ,request object (req), response object (res) for each route. Also, middleware function can either terminate the HTTP request or pass it on to another middleware function using next.

This diagram shows you how to express middleware structure.

Middleware functions perform the following

- Execte any code.
- Makes a change to the request/response object data.
- End the request-response cycle.
- Run the next command to perform the next middleware function.

Request — Response cycle
behavior of the request-response cycle is
- The user opens a browser, type the desired URL and press enter.
- The browser will make a request or make a request to the server through the specified URL.
- When the request sent to the server matches the route or URL path that has been set, code will execte.
- When the work is complete, it will response sent the values back.
- It may render or create HTML format and send it back to the browser.
- When the page has finished loading the user will see the page. Show information that has been returned.

Types of middleware, functions

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

In the case of using application-level and router-level, we can assign paths to middleware.
You can also run the middleware, the combined function, and the set of functions at the same time.

Let’s get to know more about each type in order

Application-level middleware
Middleware functions at the level application-level is a function that is executed through the app object using the command app.use () and app.method (), where method is an HTTP method, such as get post put or delete. For example: app.get ( ), app.post ( ), app.put ( ) or app.delete ( ).

Router-level middleware
Middleware Router-level functions have the same functionality as that of the Application-level. The router is a sub app, but the router is an instance of express.Router ().

Error-handling middleware
In error-handling middleware, there are 4 arguments, one called err (error). In order to define middleware in this format, we must always use all four values, although we may not need to use next. In order to maintain the correct usage patterns Otherwise It will be interpreted as a normal function middleware. Making it impossible to handle errors.

Built-in middleware
It is a function middleware that facilitates the express app development.

For example
express.static () // Calling static files such as image files, js files, css files etc.
express.json () // Convert JSON String formatted data to JSON Objext format.
express.urlencoded () // Convert data from form url encode to Object.

Third-party middleware
It is a middleware function that has been developed separately in the package, not in the Express app. Via NodeJs package manager, also known as mpm (Node Package Manager).

This is the short blogs, I would like to start talking about middleware express. and how middleware express work. I hope this blog will help you understand basics of middleware express.

--

--

Alice Richardson
Alice Richardson

Written by Alice Richardson

Software Development with a background designer.

No responses yet