If you need a visual feedback to understand when the middlewares are executed take a look to the page.
Fastify middlewares don't support the full syntax , because error handling is done inside Fastify. Furthermore methods added by Express and Restify to the enhanced versions of req
and res
are not supported in Fastify middlewares.
Also, if you are using a middleware that bundles different, smaller middlewares, such as helmet, we recommend to use the single modules to get better performances.
Remember that middlewares can be encapsulated, this means that you can decide where your middlewares should run by using register
as explained in the .
Fastify middlewares also do not expose the send
method or other methods specific to the Fastify Reply instance. This is because Fastify wraps the incoming and res
Node instances using the and Reply objects internally, but this is done after the middlewares phase. If you need to create a middleware you have to use the Node req
and res
instances. Otherwise, you can use the preHandler
hook that has the and Reply Fastify instances. For more information, see .
Restrict middleware execution to a certain path(s)
Note that this does not support routes with parameters, (eg: /user/:id/comments
) and wildcard is not supported in multiple paths.
Express middleware compatibility
Express modifies the prototype of the node core Request and Response objects heavily so Fastify cannot guarantee full middleware compatibility. Express specific functionality such as res.sendFile()
, res.send()
or express.Router()
instances will not work with Fastify. For example, cors is compatible while is not.