Middleware
Last updated
Was this helpful?
Last updated
Was this helpful?
You can create your own middlewares by following Fastify's Plugin architecture
You can write these in your /app/index.ts
file, or in your middlewares, or in your routes if you wish to apply route level
Route middlewares are middleware files that are applied to a group of routes in the api directory
From the example above, the middleware.ts file is ONLY applied to the route within that directory.
A minimum implementation of a middleware file is below:
Fastify rules for Plugins apply to this file.
Route middlewares that are available out of the box are discussed below:
All POST
, PUT
, PATCH
, and DELETE
methods automatically check the csrf of the incoming requests.
The CSRF is in both the local storage of the browser and cookie. If this does not exist in the request, the request will throw a InvalidCsrfTokenError
References:
2024-12-20 - Initial Documentation