Our great sponsors
-
SurveyJS
Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App. With SurveyJS form UI libraries, you can build and style forms in a fully-integrated drag & drop form builder, render them in your JS app, and store form submission data in any backend, inc. PHP, ASP.NET Core, and Node.js.
-
ajv
The fastest JSON schema Validator. Supports JSON Schema draft-04/06/07/2019-09/2020-12 and JSON Type Definition (RFC8927)
-
InfluxDB
Power Real-Time Data Analytics at Scale. Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.
Fastify comes with Pino by default, but logging must be enabled for it to work:
Fastify offers many additional features that we couldn't cover in this series, so check out the Fastify official documentation to learn more. Also, you can find all the code samples we used in this GitHub repository.
As mentioned in part 2 of this series, using the @fastify/express plugin is the quickest way to get your existing Express application working with Fastify. The plugin adds full Express compatibility to Fastify so that you can easily use any Express middleware — or even an entire Express application — with your Fastify instance, and it will just work with no changes required.
Since we've migrated our only application route that renders templates, the above lines are no longer needed and can be safely deleted. But you now need to configure Fastify to render the Pug template through the @fastify/view plugin. It decorates the Reply interface with a method that we'll then use to render the template.
Since Fastify supports schema validation with Ajv, the validate module is no longer required on the /shorten route, and we can specify the JSON schema directly on the route. The controllers for both routes will largely remain the same, except that the res parameter is renamed to reply as before:
We can replace this middleware with the @fastify/static plugin, which you can install through the command below:
helmet -> @fastify/helmet.
cors -> @fastify/cors.
Our Express application uses Winston and Morgan for logging HTTP requests.
To illustrate the process of migrating from Express to Fastify, we have prepared a demo application. This application utilizes Express, Mongoose, and Pug to create a URL Shortener app as follows:
{"level":"info","time":"2023-05-08T08:09:43.047Z","pid":920874,"host":"fedora","reqId":"req-1","req":{"method":"POST","url":"/shorten","hostname":"localhost:3000","remoteAddress":"127.0.0.1","remotePort":60582},"msg":"incoming request"} {"level":"debug","time":"2023-05-08T08:09:43.058Z","pid":920874,"host":"fedora","msg":"https://github.com/nvim-telescope/telescope.nvim#themes shortened to mB5Ke86"} {"level":"info","time":"2023-05-08T08:09:43.060Z","pid":920874,"host":"fedora","reqId":"req-1","res":{"statusCode":200},"responseTime":12.570792019367218,"msg":"request completed"}
Related posts
- Advanced Fastify: Hooks, Middleware, and Decorators
- Getting Started with Fastify for Node.js
- 5 Node.js security code snippets every backend developer should know
- Google OAuth2 with Fastify + TypeScript From Scratch
- To use multiple env files for each environment or not? What is your take on this? How are you implementing this?