GlenTiki / fastify-autoload

Require all plugins in a directory

Repository from Github https://github.comGlenTiki/fastify-autoloadRepository from Github https://github.comGlenTiki/fastify-autoload

fastify-autoload

Build Status  Greenkeeper badge

Require all plugins in a directory.

Install

npm i fastify fastify-autoload

Example

'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)

Custom configuration

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.

License

MIT

About

Require all plugins in a directory

License:MIT License


Languages

Language:JavaScript 99.5%Language:Shell 0.5%