Last Updated: February 25, 2016
·
1.531K
· iheartweb

A node application with hapi, bunyan & a glob loader.

Aren't weekend projects great!?

After much ado, this is the pattern I've settled on for starting
applications. Some of the npms are private, but you'll get the point.
TODO: look into https://github.com/spumko/hapi/blob/master/docs/Reference.md#hapipack

var config = require("./configs/" + process.env.NODE_ENV),
  figure = require("droppe-figure"),
  bunyan = require("bunyan"),
  applicationName,
  logger;

applicationName = config.get("applicationName");
logger = bunyan.createLogger({name: applicationName});

// Get options from the confiuration service 
figure.get("/" + applicationName + "/" + process.env).then(function (options) {
  var hapi = require('hapi'),
    server;

  // Merge local config with service config
  Object.keys(options).forEach(function (key) {
    config.set(key, options[key]);
  });

  // Make hapi
  server = new hapi.Server(
    config.get("hostname"),
    config.get("port"),
    config.get("application")
  );

  logger.info("Starting the application w/", config.get());

  // start the server
  server.start(function (error) {
    var loader;

    if (error) {
      logger.warn("Failed to start application at w/", error);
      return;
    }

    loader = require("node-glob-loader");

    server.app.logger = logger;

    // Load application routes
    loader.load(["./application/routes/*", "./application/routes/**/*"], function (exports) {
      exports(server, config);
    }).then(function () {
      logger.warn("Started application at ", server.info.uri);
    }).done();
  });
}).fail(function (error) {
  logger.warn("Failed to start application at w/", error);
}).done();

1 Response
Add your response

heh, still on the CW LinkedIn team...

over 1 year ago ·