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

标签 javascript node.js plugins hapi.js

我关注了hapi的官网,尝试了一个简单的服务器,但是失败了: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');
    });
});

它正在抛出愚蠢的错误:

/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

这是我的 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"
  }

最佳答案

根据 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.
      • 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.

插件对象是具有以下属性的对象:

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

    • server - the server object with a plugin-specificserver.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 (e.g. 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.

因此,对于您的用法,请尝试:

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

关于javascript - 如何在 Hapi.js 中正确注册插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48134494/

相关文章:

javascript - AngularJS jsonp 成功错误

plugins - 家庭网站CMS

javascript - Node.js websocket 错误 "Error: listen EADDRNOTAVAIL Error: listen EADDRNOTAVAIL"

node.js - 在Node和sqlite3中插入多行多列

javascript - 检查变量是否为空不起作用 | NodeJS、Express

plugins - 在 portlet 类中与 Liferay portlet 配置交互

css - 如何在Notepad++中格式化和缩进CSS?

javascript - 如何将从blob url获取的音频数据保存到mysql db中的文件系统和路径中

javascript - jQuery-ui slider - 如何阻止两个 slider 相互控制

javascript - 动态ajax/php选择标签