Created
March 19, 2018 10:12
-
-
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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