Skip to content

Instantly share code, notes, and snippets.

@Ethan-Arrowood
Created March 19, 2018 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ethan-Arrowood/35e54c688e290e8e6a996ccc5c711c2f to your computer and use it in GitHub Desktop.
Save Ethan-Arrowood/35e54c688e290e8e6a996ccc5c711c2f to your computer and use it in GitHub Desktop.
This is an example Fastify plugin and server from my medium article:
const fastify = require('fastify')()
const superPlugin = require('./superPlugin.js')
fastify.register(superPlugin, {
secretCode: 'JavaScript is awesome!'
})
fastify.get('/', (request, reply) => {
reply.send({ hello: 'world' })
})
fastify.listen(3000, err => {
fastify.superMethod()
})
const fp = require('fastify-plugin')
function superPlugin (fastify, opts, next) {
fastify.decorate('superMethod', () => {
console.log(`Secret code: ${opts.secretCode}`)
})
next()
}
module.exports = fp(superPlugin, {
fastify: '>=1.0.0',
name: 'super-plugin'
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment