Adds Fastify to your application.
gasket create <app-name> --plugins @gasket/plugin-fastify
npm i @gasket/plugin-fastify
Modify plugins
section of your gasket.config.js
:
module.exports = {
plugins: {
add: [
+ '@gasket/plugin-fastify'
]
}
}
All the configurations for the plugin are added under fastify
in the config:
compression
: true by default. Can be set to false if applying compression differently.excludedRoutesRegex
: Routes to be excluded based on a regextrustProxy
: Enable trust proxy option, see Fastify documentation for possible values
module.exports = {
plugins: {
add: ['@gasket/fastify']
},
fastify: {
compression: false,
excludedRoutesRegex: /^(?!\/_next\/)/,
trustProxy: true
}
}
The gasket.config.js
can contain a middleware
property, which is an array of
objects that map plugins to route or path patterns, allowing apps to tune which
middleware are triggered for which requests.
middleware: [
{
plugin:'gasket-plugin-example', // Name of the Gasket plugin
paths: ['/api']
},
{
plugin:'@some/gasket-plugin-example',
paths: [/\/default/]
},
{
plugin: '@another/gasket-plugin-example',
paths: ['/proxy', /\/home/]
}
]
Executed when the fastify
server has been created, it will apply all returned
functions as middleware.
module.exports = {
hooks: {
/**
* Add Fastify middleware
*
* @param {Gasket} gasket The Gasket API
* @param {Fastify} app - Fastify app instance
* @returns {function|function[]} middleware(s)
*/
middleware: function (gasket, app) {
return require('x-xss-protection')();
}
}
}
You may also return an Array
to inject more than one middleware.
Executed after the middleware
event for when you need full control over
the fastify
instance.
module.exports = {
hooks: {
/**
* Update Fastify app instance
*
* @param {Gasket} gasket The Gasket API
* @param {Fastify} fastify Fastify app instance
* @returns {function|function[]} middleware(s)
*/
fastify: async function (gasket, fastify) {
}
}
}
Executed after the fastify
event. All middleware functions returned from this
hook will be applied to Fastify.
module.exports = {
hooks: {
/**
* Add Fastify error middlewares
*
* @param {Gasket} gasket The Gasket API
* @returns {function|function[]} error middleware(s)
*/
errorMiddleware: function (gasket) {
}
}
}
This plugins hooks the createServers lifecycles from @gasket/plugin-https.