我关注了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
- iftrue
, subsequent registrations of the same plugin are skipped without error. Cannot be used with plugin options. Defaults tofalse
. If not set totrue
, 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 theprefix
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-mostvhost
overrides the any nested configuration.Return value: none.
插件对象
是具有以下属性的对象:
register
- (required) the registration function with the signatureasync function(server, options)
where:
server - the server object with a plugin-specific
server.realm`.options
- any options passed to the plugin during registration viaserver.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) iftrue
, allows the plugin to be registered multiple times with the same server. Defaults tofalse
.
dependencies
- (optional) a string or an array of strings indicating a plugin dependency. Same as setting dependencies viaserver.dependency()
.
once
- (optional) iftrue
, will only register the plugin once per server. If set, overrides theonce
option passed toserver.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/