hapijs / glue

Server composer for hapi.js
Other
245 stars 62 forks source link

server events postRegister #92

Closed jchoi2x closed 7 years ago

jchoi2x commented 7 years ago

I need to write a plugin that is registered last, after all server-methods are added, without having to explicitly know exactly which plugins to list as dependencies. I see that there is a way to add preRegister but there is no way to add a postRegister; which still isn't how I'd like to use the plugin .

I would like to use something like the following in the plugin, but there isn't a server event for postRegister

const internals = module.exports = {};

internals.register = (server, options, next) => {
   server.on('postRegister', (server, next) => {
       // wrap all server methods
      ......
      server.decorate('server', 'preWrapper', pres);
   });
}

Adding the functionality is no problem, but if I DO make a PR for this would you guys consider merging it?

csrl commented 7 years ago

Just do whatever you need to do in the compose callback. Like calling server.register(veryLastPlugin). Also, the plugins are loaded in array order, so you can also just place your plugin last in the registrations array.

Glue.compose(manifest, options, (err, server) => {

    if (err) {
        throw err;
    }

    // do whatever you want here, before calling server.start.  eg.  server.register or server.decorate

    server.start(() => {

        console.log('hapi days!');
    });
});
lock[bot] commented 4 years ago

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.