Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly register plugins in Hapi.js

Tags:

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"
  }
like image 365
Humoyun Ahmad Avatar asked Jan 07 '18 05:01

Humoyun Ahmad


People also ask

What is plugins in Hapi JS?

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.

What is Hapi glue?

A server composer for hapi. Glue provides configuration based composition of hapi's Server object. Specifically it wraps. server = Hapi.server(Options)

What is Hapi swagger?

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/.


1 Answers

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-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.

So, for your usage, try:

const Inert = require('inert');
const Vision = require('vision');
await server.register([Inert.plugin, Vision.plugin]);
like image 83
John Manko Avatar answered Sep 20 '22 13:09

John Manko