Get Hapi to Register @Hapi/Good Plugin Only Once

2019-08-24 04:00发布

问题:

Hapi (v17 & 18) states that I can specify an option on server.register to make the plugin on get initialized once, regardless of however many times server.register is called with that plugin - link to docs. However, I have been unable to get this to work.

I have tried putting options an element on the object that gets passed into server.register. When I try to run the server, I get the error [1] "once" conflict with forbidden peer "options". This leads me to believe that it must go in the options object.

await server.register({
    plugin: require('@hapi/good'),
    options: { /* omitted */ },
    once: true
  });

I try to add it to the options object that gets passed into server.register. However, I get this error [1] "once" is not allowed.

await server.register({
    plugin: require('@hapi/good'),
    options: {
      once: true,
      /* omitted */
    },
  });

I have not been able to find an example of this option anywhere online besides this github issue. However, that only covers the routes.

回答1:

@hapi/good uses Joi to check what valid options can be passed in to the plugin. Check be read here in the source code. It seems that @hapi/good prohibits the use of routes and once because it denies all except the allowed entities by default. This seems to be an issue though because once is a configuration of the plugin for the server - not for the plugin.