The right way to work with hapi is to build your application using plugins. Plugins provide a way to organize your code into logical components and then put them together in different combinations or deploy them in different configurations. While some plugins are published as general purpose utilities (e.g. adding authentication), you should think of plugins as a way to break your code into pieces.
Now that we have a basic server with one endpoint, we want to move the creation of the endpoint to a plugin,
and then register that plugin with the server. Creating a new file lib/version.js
and move the /version
endpoint there,
wrapped in the plugin register()
function.
Then change our current lib/index.js
to require the new file and register the version plugin with our server.
Remember to follow the style guide, and ask any questions in the comments of the issue created for the assignment. If you are not sure how to get your fork back in sync with the current updated code, use the git guide.
require('./version') should be declared at the top and assigned to
Version`.- no need to wrap the plugin in
{ register: Version }
. It is enough to just pass Version directly to theregister()
function. - no need for specifying the plugin version in the attributes. This is an internal plugin, not a publicly published one.
server.start()
must be inside the callback toserver.register()
otherwise the server will start before all the routes are added.- Is there a preferred pattern for handline async when registering plugins?
- You can pass an array if they all share the same (or lack of) options.
- Otherwise, you can use the glue module or create your own utility. Glue is added to the project in assignment5.
source for Helps: Discussion between @hueniverse and @TheAlphaNerd. Original Assignment2