Code Monkey home page Code Monkey logo

fastify-autoload's Introduction

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.

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

If you have some files in the folder that you'd like autoload to skip you can set ignorePattern option to a regex. If that matches a file it will not load it.

// index.js
fastify.register(AutoLoad, {
  dir: path.join(__dirname, 'foo'),
  options: { prefix: '/defaultPrefix' }
  ignorePattern: /.*(test|spec).js/
})

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.