How to Write Node.js REST Middleware with Express.js with TypeScript

Note: This post is part of Learn How to Use TypeScript With Node.js and Express.js series. Click here to see the first post of the series.

What is a Middleware in Node.js/Express.js?

A middleware is essential in the development of an application, in our case an API. As the name suggests, middleware is a process that occurs in the middle of a cycle. In the case of node.js and express.js, middlewares are in the middle of the client request and the event handler that sends a response back to the client. Middlewares have access to the request and the response objects, which allows developers to perform tasks such as validating the data, parsing data, among others.

The most common types of middleware are the following:

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

We are going to use one of each middleware in our application and learn by example. Let’s start working with third-party middlewares.

Share this article or checkout our social media channels!