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"
}
Essentially, a plugin is an object with a register property that returns a function with the signature function (server, options, next) . Further, the register function has an attributes object that contains meta data about your plugin to provide some extra data for hapi.
A server composer for hapi. Glue provides configuration based composition of hapi's Server object. Specifically it wraps. server = Hapi.server(Options)
Swagger documentation (via hapi-swagger) is automatically generated for all endpoints and can be viewed by pointing a browser at the server URL. If you use the intro script this will be http://localhost:8080/.
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
- 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.
A plugin object
is an object with the following properties:
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.
So, for your usage, try:
const Inert = require('inert');
const Vision = require('vision');
await server.register([Inert.plugin, Vision.plugin]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With