简体   繁体   中英

How to properly register plugins in Hapi.js

I followed hapi's official website and tried a simple server, but failed: I cannot register plugins,

var Hapi = require('hapi');

var server = new Hapi.Server();

server.connection({port: 4004});

server.register([require('inert'), require('vision')], (err) => {
    if (err) {
        throw err;
    }

    server.start(err => {
        console.log('server started');
    });
});

It is throwing foollowing error:

/Users/apple/Documents/node_projects/hapijon/testjon/ch4_routes_and_handlers/node_modules/hapi/lib/plugin.js:219
        if (plugin.register.register) {                             // Required plugin
                            ^

TypeError: Cannot read property 'register' of undefined
    at module.exports.internals.Server.internals.Plugin.register (/Users/apple/Documents/node_projects/hapijon/testjon/ch4_routes_and_handlers/node_modules/hapi/lib/plugin.js:219:29)
    at Object.<anonymous> (/Users/apple/Documents/node_projects/hapijon/testjon/ch4_routes_and_handlers/tess.js:7:8)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Function.Module.runMain (module.js:676:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3

Here is my package.json:

  "dependencies": {
    "accept": "^2.1.4",
    "hapi": "^14.2.0",
    "inert": "^5.0.1",
    "iron": "^5.0.4",
    "vision": "^5.3.0",
    "wreck": "^14.0.2"
  }

According to v17 docs at https://hapijs.com/api#server.register() :

await server.register(plugins, [options])

Registers a plugin where:

  • plugins - one or an array of:

    • a plugin object .
    • an object with the following:
      • plugin - a plugin object.
      • options - (optional) options passed to the plugin during registration.
      • once , routes - (optional) plugin-specific registration options as defined below.
  • options - (optional) registration options (different from the options passed to the registration function):

    • once - if true , subsequent registrations of the same plugin are skipped without error. Cannot be used with plugin options. Defaults to false . If not set to true , an error will be thrown the second time a plugin is registered on the server.
    • routes - modifiers applied to each route added by the plugin:
      • prefix - string added as prefix to any route path (must begin with '/' ). If a plugin registers a child plugin the prefix is passed on to the child or is added in front of the child-specific prefix.
      • vhost - virtual host string (or array of strings) applied to every route. The outer-most vhost overrides the any nested configuration.

Return value: none.

A plugin object is an object with the following properties:

  • register - (required) the registration function with the signature async function(server, options) where:

    • server - the server object with a plugin-specific server.realm`.
    • options - any options passed to the plugin during registration via server.register() .
  • name - (required) the plugin name string. The name is used as a unique key. Published plugins (eg published in the npm registry) should use the same name as the name field in their 'package.json' file. Names must be unique within each application.

  • version - (optional) plugin version string. The version is only used informatively to enable other plugins to find out the versions loaded. The version should be the same as the one specified in the plugin's 'package.json' file.

  • multiple - (optional) if true , allows the plugin to be registered multiple times with the same server. Defaults to false .

  • dependencies - (optional) a string or an array of strings indicating a plugin dependency. Same as setting dependencies via server.dependency() .

  • once - (optional) if true , will only register the plugin once per server. If set, overrides the once option passed to server.register() . Defaults to no override.

So, for your usage, try:

const Inert = require('inert');
const Vision = require('vision');
await server.register([Inert.plugin, Vision.plugin]);

我会将hapi的版本更新到最新版本,这两个插件的版本似乎只适用于最新版本的hapi。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM