1 | <h1 align="center">Fastify</h1>
|
2 |
|
3 | ## Middleware
|
4 |
|
5 | Fastify provides an asynchronous [middleware engine](https://github.com/fastify/middie) out-of-the-box, which is compatible with [Express](https://expressjs.com/) and [Restify](http://restify.com/) middleware.
|
6 |
|
7 | *For help with understanding when middleware is executed, take a look at the [lifecycle](https://github.com/fastify/fastify/blob/master/docs/Lifecycle.md) page.*
|
8 |
|
9 | Fastify middleware don't support the full syntax `middleware(err, req, res, next)`, because error handling is done inside Fastify.
|
10 | Furthermore, methods added by Express and Restify to the enhanced versions of `req` and `res` are not supported in Fastify middlewares.
|
11 |
|
12 | Also, if you are using middleware that bundles different, smaller middleware, such as [*helmet*](https://helmetjs.github.io/), we recommend using the single modules for better performance.
|
13 |
|
14 | ```js
|
15 | fastify.use(require('cors')())
|
16 | fastify.use(require('dns-prefetch-control')())
|
17 | fastify.use(require('frameguard')())
|
18 | fastify.use(require('hide-powered-by')())
|
19 | fastify.use(require('hsts')())
|
20 | fastify.use(require('ienoopen')())
|
21 | fastify.use(require('x-xss-protection')())
|
22 | ```
|
23 |
|
24 | or, in the specific case of *helmet*, you can use the [*fastify-helmet*](https://github.com/fastify/fastify-helmet) [plugin](Plugins.md), which is an optimized helmet integration for fastify:
|
25 |
|
26 | ```js
|
27 | const fastify = require('fastify')()
|
28 | const helmet = require('fastify-helmet')
|
29 |
|
30 | fastify.register(helmet)
|
31 | ```
|
32 |
|
33 | Remember that middleware can be encapsulated, this means that you can decide where your middleware should run by using `register` as explained in the [plugins guide](https://github.com/fastify/fastify/blob/master/docs/Plugins-Guide.md).
|
34 |
|
35 | Fastify middleware also do not expose the `send` method or other methods specific to the Fastify [Reply](./Reply.md#reply) instance. This is because Fastify wraps the incoming `req` and `res` Node instances using the [Request](./Request.md#request) and [Reply](./Reply.md#reply) objects internally, but this is done after the middleware phase. If you need to create middleware, you have to use the Node `req` and `res` instances. Otherwise, you can use the `preHandler` hook which already has the [Request](./Request.md#request) and [Reply](./Reply.md#reply) Fastify instances. For more information, see [Hooks](./Hooks.md#hooks).
|
36 |
|
37 | <a name="restrict-usage"></a>
|
38 | #### Restrict middleware execution to a certain path(s)
|
39 | If you need to run a middleware only under certain path(s), just pass the path as first parameter to `use` and you are done!
|
40 |
|
41 | *Note that this does not support routes with parameters, (eg: `/user/:id/comments`) and wildcards are not supported in multiple paths.*
|
42 |
|
43 | ```js
|
44 | const path = require('path')
|
45 | const serveStatic = require('serve-static')
|
46 |
|
47 | // Single path
|
48 | fastify.use('/css', serveStatic(path.join(__dirname, '/assets')))
|
49 |
|
50 | // Wildcard path
|
51 | fastify.use('/css/*', serveStatic(path.join(__dirname, '/assets')))
|
52 |
|
53 | // Multiple paths
|
54 | fastify.use(['/css', '/js'], serveStatic(path.join(__dirname, '/assets')))
|
55 | ```
|
56 |
|
57 | <a name="express-middleware"></a>
|
58 | #### Express middleware compatibility
|
59 | 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](https://github.com/expressjs/cors) is compatible while [passport](https://github.com/jaredhanson/passport) is not.
|