July 2017
Intermediate to advanced
656 pages
16h 1m
English
Let's copy the app folder from our main recipe to the custom-plugin-app and create a plugins folder with an answer.js file:
$ cp -fr app custom-plugin-app $ cd custom-plugin-app $ mkdir plugins $ touch plugins/answer.js
We'll make the contents of plugins/answer.js look like so:
module.exports = answer
function answer (server, options, next) {
server.ext('onPreResponse', (request, reply) => {
request.response.header('X-Answer', 42)
reply.continue()
})
next()
}
answer.attributes = {name: 'answer'}
The next callback is supplied to allow for any asynchronous activity. We call it to let Hapi know we've finished setting up the plugin. Under the hood Hapi would call the server.register callback once all the plugins had called ...