'use strict';

module.exports = function(sails) {
  return {
    // Pause sails lifting until this hook has completed initializing
    ready: false,
    initialize: function(next) {
      sails.newrelic = global.newrelic;
      delete global.newrelic;
      sails.on('router:route', function(route) {
        if (route.req.options && route.req.options.controller) {
          return sails.newrelic.setControllerName(route.req.options.controller, route.req.options.action);
        }
      });
      return next();
    }
  };
};

'use strict';

module.exports = function(sails) {
  return {
    // Pause sails lifting until this hook has completed initializing
    ready: false,
    initialize: function(next) {
      sails.newrelic = global.newrelic;
      delete global.newrelic;
      sails.on('router:route', function(route) {
        if (sails.newRelic && route.req.options && route.req.options.controller) {
          return sails.newrelic.setControllerName(route.req.options.controller, route.req.options.action);
        }
      });
      return next();
    }
  };
};

Allow hook to work without disrupting `sails lift`
Allow hook to work without disrupting `sails lift`

The hook works great on environments that use `node app.js` to start the application, but causes breaking errors when using `sails lift`. I'm seeing this when developing locally vs on production.

Adding this simple check for the existence of the `sails.newRelic` object will prevent errors when using `sails.lift`.