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.
MIT