I'm approaching to this framework for node.js for several reasons. Simplicity, great modularity and a fast configuration out of the box. I have soon encountered the concept of Pack which I have never seen during my experience in the learning of express.js framework. From the official guide the following example:
var Good = require('good');
server.pack.register(Good, function (err) {
if (err) {
throw err; // something bad happened loading the plugin
}
server.start(function () {
server.log('info', 'Server running at: ' + server.info.uri);
});
});
They says about Pack:
Packs are a way for hapi to combine multiple servers into a single unit, and are designed to give a unified interface when working with plugins.
This concept is weird for me. How many times do we work with different servers in a project? In addition is not clear for me whether I should call pack every time to register a plugins in hapi.
Update: This is pre v8 api code, the way to register a plugin has been changed. (call register
directly on the server
)
This concept is weird for me. How many times do we work with different servers in a project?
One example is when you have an api and a web server. These are usually developed separately, often in separate repositories. You could then create a third project which combines these plugins:
var Hapi = require('hapi');
var manifest = {
servers: [
{
host: 'localhost',
port: 8000,
options: {
labels: 'api',
cors: true
}
},
{
host: 'localhost',
port: 8001,
options: {
labels: 'web'
}
}
],
plugins: {
'./example-api': [{select: 'api'}],
'./example-web': [{select: 'web'}]
}
};
Hapi.Pack.compose(manifest, function(err, pack) {
pack.start();
});
In addition is not clear for me whether I should call ever time pack to register a plugins in hapi.
Yes, you need to call pack.register()
when you want to register a plugin. However you can register more plugins at once:
plugin.register([
require('crumb'),
require('hapi-auth-cookie')
], function (err) {
// Error handling
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With