Require all plugins in a directory.
npm i fastify fastify-autoload
'use strict'
const Fastify = require('fastify')
const AutoLoad = require('fastify-autoload')
const fastify = Fastify()
fastify.register(AutoLoad, {
dir: path.join(__dirname, 'foo')
})
fastify.listen(3000)
Plugins in the loaded folder could add an autoPrefix
property, so that
a prefix is applied automatically when loaded with fastify-autoload
:
module.exports = function (fastify, opts, next) {
// when loaded with autoload, this will be exposed as /something
fastify.get('/', (request, reply) => {
reply.send({ hello: 'world' })
})
}
// optional
module.exports.autoPrefix = '/something'
If you need to disable the auto loading for a specific plugin, add autoload = false
property.
module.exports = function (fastify, opts, next) {
// your plugin
}
// optional
module.exports.autoload = false
If you want to pass some custom options to the registered plugins via fastify-autoload
, use the options
key:
fastify.register(AutoLoad, {
dir: path.join(__dirname, 'foo'),
options: { foo: 'bar' }
})
Note that options will be passed to all loaded plugins.
You can set the prefix option in the options passed to all plugins to set them all default prefix.
When plugins get passed prefix
as a default option, the autoPrefix
property gets appended to them.
This means you can load all plugins in a folder with a default prefix.
// index.js
fastify.register(AutoLoad, {
dir: path.join(__dirname, 'foo'),
options: { prefix: '/defaultPrefix' }
})
// /foo/something.js
module.exports = function (fastify, opts, next) {
// your plugin
}
// optional
module.exports.autoPrefix = '/something'
// routes can now be added to /defaultPrefix/something
If you have a plugin in the folder you don't want the default prefix applied to, you can add the prefixOverride
key:
// index.js
fastify.register(AutoLoad, {
dir: path.join(__dirname, 'foo'),
options: { prefix: '/defaultPrefix' }
})
// /foo/something.js
module.exports = function (fastify, opts, next) {
// your plugin
}
// optional
module.exports.prefixOverride = '/overriddenPrefix'
// routes can now be added to /overriddenPrefix
If you have a plugin in the folder you don't want the any prefix applied to, you can set prefixOverride = ''
:
// index.js
fastify.register(AutoLoad, {
dir: path.join(__dirname, 'foo'),
options: { prefix: '/defaultPrefix' }
})
// /foo/something.js
module.exports = function (fastify, opts, next) {
// your plugin
}
// optional
module.exports.prefixOverride = ''
// routes can now be added without a prefix
MIT