API with NestJS #89. Replacing Express with Fastify

Express JavaScript NestJS

This entry is part 89 of 121 in the API with NestJS

By default, NestJS uses Express under the hood. Moreover, since Express is very popular, NestJS can choose from a broad set of compatible third-party solutions.

A significant advantage of NestJS is that it is framework-independent. Instead of using Express, we can use an adapter using another library with a similar request/response pipeline. The most popular implementation besides Express is the Fastify adapter.

Check out this repository to see the full code from this article.

Introducing Fastify

The main selling point of Fastify is performance. We can find various comparisons on the web that prove that Fastify handles HTTP requests faster than Express.

However, there is a big chance that Express is not the bottleneck in your application. Instead, optimizing how we use our database and caching can yield great results.

If you want to know more about caching with NestJS, check out the following articles:

Using Fastify with NestJS

At first glance, it’s very straightforward to start using Fastify with NestJS. To do that, we only need to modify our file.

main.ts

When we do the above, NestJS starts using Fastify as its HTTP provider, and we don’t need to provide any additional configuration.

However, one of the biggest strengths of Express is its wide selection of compatible libraries. When using Fastify, we must ensure that the packages we use are compatible or use alternatives developed with Fastify in mind.

We also need to remember that since Express is the default solution for NestJS, all of the libraries maintained by the NestJS team are usually developed with Express in mind. Therefore, if we decide to go with Fastify, we must brace ourselves to deal with some incompatibilities.

Things to watch out for when switching to Fastify

Modifying our file is enough to start using Fastify. Although that’s the case, let’s go deeper and investigate some real-life scenarios to see what it’s like using Fastify.

In the third part of this series, we’ve implemented authentication using bcrypt, Passport, JWT, and cookies. Since many projects include some authentication, it is a good starting point to learn how to use Fastify with NestJS.

Accessing the request and response objects

When using Express, we can easily access the and object using the correct decorators.

categories.controller.ts

When we use Fastify, we need to use different interfaces for the above objects.

categories.controller.ts

While in the above example, Express and Fastify work the same, this is not always the case. For example, to set a header in the response, we need to use the function instead of .

authentication.controller.ts

Thanks to using we can return the data from the above method and let NestJS send the data. Without that, we would need to call the method instead.

Working with cookies

The cookie-parser library is a very popular middleware ready to use with Express. However, when using Fastify, we need to find an alternative.

Fortunately, the library is straightforward. For our application to support cookies, we need to modify our file and call the method.

main.ts

Passport

In this series, we’ve used the Passport library to avoid implementing all aspects of authentication manually. Sadly, the library does not support Fastify officially.

There is the package, but it’s not very popular. Unfortunately, it integrates with Passport differently, and guards built into NestJS might not work out of the box with it.

Thankfully, works fine with Passport as long as we use simple JWT-based authentication.

authentication.controller.ts

We can access thanks to using the library.

Please notice that above we use instead of . This is because using the latter would cause TypeScript to complain that is incompatible with .

While JWT-based authentication works fine, we might encounter issues when implementing OAuth. Thankfully, the official Discord channel of NestJS is a great place to get help with such problems. For example, Jay McDoniel, who is a part of the core NestJS team, suggests adding the following snippet to our file if we want to make work with OAuth and Fastify:

In the above code we try to make Fastify more compatible with how the and objects work in Express.

Summary

In this article, we’ve replaced Express with Fastify and achieved a fully-working application that includes authentication. While configuring NestJS to use Fastify is very simple, working with Fastify might not be that convenient. When switching to Fastify, we might increase the performance of our application, but we need to be aware of the disadvantages.

There is a big community behind Express, and it shows. If your application requires top-notch performance, it’s worth giving Fastify a try.

Series Navigation<< API with NestJS #88. Testing a project with raw SQL using integration testsAPI with NestJS #90. Using various types of SQL joins >>
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments