Skip to content

Server Setup

jstrimpel edited this page Oct 13, 2014 · 5 revisions

Hapi powers the Lazo server by creating a pack of Hapi servers that can be configured using conf.json. In some cases it might be necessary to have direct access to this pack or Hapi itself. For instance, you might want to use a Hapi plugin like Good for monitoring the Lazo server. This can be accomplished using the lazoServer module.

lazoServer Module

The lazoServer base class module has two methods.

app/server/server.js should extend lazoServer (see examples below)

setup

The setup method can be used to add plugins to the pack, spin up another Hapi server, or anything else before the Lazo server pack is started.

define(['lazoServer'], function (LazoServer) {

    'use strict';

    return LazoServer.extend({
        // register good with lazo server pack
        // hapi - a reference to the hapi module itself
        // pack - the lazo server pack
        // servers - the servers that belong to the lazo pack
        // options.success and options.error
        setup: function (hapi, pack, servers, options) {

            pack.register({
                plugin: require('good'),
                options: {
                    // see good documentation, https://github.com/hapijs/good
                }
            }, function (err) {
                if (err) {
                    LAZO.logger.error('[server.setup]', 'Error initializing monitoring: ' + err);
                    options.success();
                } else {
                    LAZO.logger.info('[server.setup]', 'Monitoring initialized');
                    // save a reference for use in lazoServer.afterInitialize
                    options.success();
                }
            });
        }

    });

});

afterInitialize

The afterInitialize method is called after the Lazo server pack has been started. Any code you want executed after the pack has been started can be executed in this method.

define(['lazoServer'], function (LazoServer) {

    'use strict';

    return LazoServer.extend({

        // same arguments as lazoServer.setup
        afterInitialize: function (hapi, pack, servers, options) {
            // do something after the lazo server pack is started
        }

    });

});