Hapi.js download plugins in order

I am new to Hapi.js and I am stuck trying to figure out how we can load plugins in order in setting up Hapi.js.

For example: I have 2 plugins Plugin1 and Plugin2 . Assume that Plugin2 is dependent on Plugin1 and cannot work until Plugin1 is executed .

It seems that loading these plugins is done using two separate server.register methods or with a single server. Register (with an array of plug-ins) seems to be executing parallel code ...

So can someone help me how can I load the plugins in order ... thanks in advance

+4
source share
2 answers

You will want to use as a solution server.dependency.

With it, you can declare the plugin dependent on another and if there is no dependency (or you accidentally create a circular dependency), your server will throw.

At the same time, you have the opportunity to use the function afterto delay the execution of code in Plugin2 , which should wait for Plugin1 to load .

"" " glue github, " - " aqua .

+6

.

Glue. :

var Glue = require('glue');

var manifest = {
    server: {
        cache: 'redis'
    },
    connections: [
        {
            port: 8000,
            labels: ['web']
        },
        {
            port: 8001,
            labels: ['admin']
        }
    ],
    plugins: [
        { 'Plugin1': null },
        { 'Plugin2': null }
    ]
};


var options = {
    relativeTo: __dirname
};

Glue.compose(manifest, options, function (err, server) {

    if (err) {
        throw err;
    }
    server.start(function () {

        console.log('Hapi days!');
    });
});

, :

server.register(require('Plugin1'), function (err) {

    server.register(require('Plugin2'), function (err) {

        server.start(function () {

            console.log('Hapi days!');
        });
    });
});

, hapi . server.dependency(), . Plugin2 :

var ready = function (server, next) {

    server.route({
        ...
    });

    next();
};

exports.register = function (server, options, next) {

    server.dependency('Plugin1', ready);
    next();
};

exports.register.attributes = { 
    name: 'Plugin2',
    version: '0.0.1'
};

. , .

+3

All Articles