简体   繁体   English

如何在Hapi.js中正确注册插件

[英]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, 我跟着hapi的官方网站试过一个简单的服务器但是失败了:我无法注册插件,

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: 这是我的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() : 根据https://hapijs.com/api#server.register()上的v17文档:

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. plugin - 一个插件对象。
      • options - (optional) options passed to the plugin during registration. options - 在注册期间传递给插件的(可选)选项。
      • once , routes - (optional) plugin-specific registration options as defined below. onceroutes - (可选)特定于插件的注册选项,如下所述。
  • options - (optional) registration options (different from the options passed to the registration function): options - (可选)注册选项(与传递给注册函数的选项不同):

    • once - if true , subsequent registrations of the same plugin are skipped without error. once - 如果为true ,则跳过相同插件的后续注册而不会出错。 Cannot be used with plugin options. 不能与插件选项一起使用。 Defaults to false . 默认为false If not set to true , an error will be thrown the second time a plugin is registered on the server. 如果未设置为true ,则第二次在服务器上注册插件时将引发错误。
    • routes - modifiers applied to each route added by the plugin: routes - 应用于插件添加的每个路由的修饰符:
      • prefix - string added as prefix to any route path (must begin with '/' ). prefix - 作为前缀添加到任何路由路径的字符串(必须以'/'开头)。 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. 如果插件注册子插件,则prefix将传递给子节点,或者添加到子节点prefix前面。
      • vhost - virtual host string (or array of strings) applied to every route. vhost - 应用于每个路由的虚拟主机字符串(或字符串数​​组)。 The outer-most vhost overrides the any nested configuration. 最外层的vhost会覆盖任何嵌套配置。

Return value: none. 返回值:无。

A plugin object is an object with the following properties: plugin object是具有以下属性的对象:

  • register - (required) the registration function with the signature async function(server, options) where: register - (必需)具有签名async function(server, options)的注册功能,其中:

    • server - the server object with a plugin-specific server.realm`. server - the server object with a plugin-specific server.realm` server - the server object with a plugin-specific
    • options - any options passed to the plugin during registration via server.register() . options - 在注册期间通过server.register()传递给插件的任何选项。
  • name - (required) the plugin name string. name - (必需)插件名称字符串。 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. 已发布的插件(例如,在npm注册表中发布)应使用与其“package.json”文件中的名称字段相同的名称。 Names must be unique within each application. 每个应用程序中的名称必须是唯一的。

  • version - (optional) plugin version string. version - (可选)插件版本字符串。 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. 版本应该与插件的'package.json'文件中指定的版本相同。

  • multiple - (optional) if true , allows the plugin to be registered multiple times with the same server. multiple - (可选)如果为true ,则允许插件在同一服务器上多次注册。 Defaults to false . 默认为false

  • dependencies - (optional) a string or an array of strings indicating a plugin dependency. dependencies - (可选)表示插件依赖项的字符串或字符串数​​组。 Same as setting dependencies via server.dependency() . 与通过server.dependency()设置依赖关系相同。

  • once - (optional) if true , will only register the plugin once per server. once - (可选)如果为true ,则每个服务器只注册一次插件。 If set, overrides the once option passed to server.register() . 如果设置,则覆盖传递给server.register()once选项。 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。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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