strong-tie/inbound-calls
0
1<h1 align="center">Fastify</h1>2 3## Middleware4 5Starting with Fastify v3.0.0, middleware is not supported out of the box and6requires an external plugin such as7[`@fastify/express`](https://github.com/fastify/fastify-express) or8[`@fastify/middie`](https://github.com/fastify/middie).9 10 11An example of registering the12[`@fastify/express`](https://github.com/fastify/fastify-express) plugin to `use`13Express middleware:14 15```js16await fastify.register(require('@fastify/express'))17fastify.use(require('cors')())18fastify.use(require('dns-prefetch-control')())19fastify.use(require('frameguard')())20fastify.use(require('hsts')())21fastify.use(require('ienoopen')())22fastify.use(require('x-xss-protection')())23```24 25You can also use [`@fastify/middie`](https://github.com/fastify/middie), which provides26support for simple Express-style middleware but with improved performance:27 28```js29await fastify.register(require('@fastify/middie'))30fastify.use(require('cors')())31```32 33Remember that middleware can be encapsulated; this means that you can decide34where your middleware should run by using `register` as explained in the35[plugins guide](../Guides/Plugins-Guide.md).36 37Fastify middleware does not expose the `send` method or other methods specific to38the Fastify [Reply](./Reply.md#reply) instance. This is because Fastify wraps39the incoming `req` and `res` Node instances using the40[Request](./Request.md#request) and [Reply](./Reply.md#reply) objects41internally, but this is done after the middleware phase. If you need to create42middleware, you have to use the Node `req` and `res` instances. Otherwise, you43can use the `preHandler` hook that already has the44[Request](./Request.md#request) and [Reply](./Reply.md#reply) Fastify instances.45For more information, see [Hooks](./Hooks.md#hooks).46 47#### Restrict middleware execution to certain paths48<a id="restrict-usage"></a>49 50If you need to only run middleware under certain paths, just pass the path as51the first parameter to `use` and you are done!52 53*Note that this does not support routes with parameters, (e.g.54`/user/:id/comments`) and wildcards are not supported in multiple paths.*55 56```js57const path = require('node:path')58const serveStatic = require('serve-static')59 60// Single path61fastify.use('/css', serveStatic(path.join(__dirname, '/assets')))62 63// Wildcard path64fastify.use('/css/(.*)', serveStatic(path.join(__dirname, '/assets')))65 66// Multiple paths67fastify.use(['/css', '/js'], serveStatic(path.join(__dirname, '/assets')))68```69 70### Alternatives71 72Fastify offers some alternatives to the most commonly used middleware, such as73[`@fastify/helmet`](https://github.com/fastify/fastify-helmet) in case of74[`helmet`](https://github.com/helmetjs/helmet),75[`@fastify/cors`](https://github.com/fastify/fastify-cors) for76[`cors`](https://github.com/expressjs/cors), and77[`@fastify/static`](https://github.com/fastify/fastify-static) for78[`serve-static`](https://github.com/expressjs/serve-static).79 