ActionHero

ActionHero

new ActionHero()

Source:

The ActionHero module.

Classes

Action
CLI
Connection
Initializer
Server
Task

Type Definitions

ActionCallback(data)

Source:
See:

This callback is displayed as part of the Requester class.

Parameters:
Name Type Description
data Object

The data object.

ActionMiddleware

Source:
See:
Properties:
Name Type Description
name string

Unique name for the middleware.

global Boolean

Is this middleware applied to all actions?

priority Number

Module load order. Defaults to api.config.general.defaultMiddlewarePriority.

preProcessor ActionHero~ActionCallback

Called berore the action runs. Has access to all params, before sanitizartion. Can modify the data object for use in actions.

postProcessor ActionHero~ActionCallback

Called after the action runs.

Middleware definition for Actions

Type:
  • Object
Example
var middleware = {
  name: 'userId checker',
  global: false,
  priority: 1000,
  preProcessor: function(data, next){
    if(!data.params.userId){
      next(new Error('All actions require a userId') );
    }else{
      next();
    }
  },
  postProcessor: function(data, next){
    if(data.thing.stuff == false){
      data.toRender = false;
    }
    next(error);
  }
}

api.actions.addMiddleware(middleware);

ChatMiddleware

Source:
See:
Properties:
Name Type Description
name string

Unique name for the middleware.

priority Number

Module load order. Defaults to api.config.general.defaultMiddlewarePriority.

join ActionHero~ChatRoomCallback

Called when a connection joins a room.

leave ActionHero~ChatRoomCallback

Called when a connection leaves a room.

onSayReceive ActionHero~ChatSayCallback

Called when a connection says a message to a room.

say ActionHero~ChatSayCallback

Called when a connection is about to recieve a say message.

Middleware definition for processing chat events. Can be of the

Type:
  • Object
Example
var chatMiddleware = {
  name: 'chat middleware',
  priority: 1000,
  join: (connection, room) => {
    // announce all connections entering a room
    api.chatRoom.broadcast({}, room, 'I have joined the room: ' + connection.id, callback)
  },
  leave:(connection, room, callback) => {
    // announce all connections leaving a room
    api.chatRoom.broadcast({}, room, 'I have left the room: ' + connection.id, callback)
  },
  // Will be executed once per client connection before delivering the message.
  say: (connection, room, messagePayload) => {
    // do stuff
    api.log(messagePayload)
  },
  // Will be executed only once, when the message is sent to the server.
  onSayReceive: (connection, room, messagePayload) => {
    // do stuff
    api.log(messagePayload)
  }
}

api.chatRoom.addMiddleware(chatMiddleware)

ChatRoomCallback(connection, room)

Source:
See:

This callback is displayed as part of the Requester class.

Parameters:
Name Type Description
connection Object

The connection being created/destroyed.

room string

The room being chatted within.

ChatSayCallback(connection, room, messagePayload)

Source:
See:

This callback is displayed as part of the Requester class.

Parameters:
Name Type Description
connection Object

The connection being created/destroyed.

room string

The room being chatted within.

messagePayload Object

The message & metadata.

ConnectionCallback(connection)

Source:
See:

This callback is displayed as part of the Requester class.

Parameters:
Name Type Description
connection Object

The connection being created/destroyed.

ConnectionMiddleware

Source:
See:
Properties:
Name Type Description
name string

Unique name for the middleware.

priority Number

Module load order. Defaults to api.config.general.defaultMiddlewarePriority.

create ActionHero~ConnectionCallback

Called for each new connection when it is created.

destroy ActionHero~ConnectionCallback

Called for each connection before it is destroyed.

Middleware definition for processing connection events

Type:
  • Object
Example
var connectionMiddleware = {
  name: 'connection middleware',
  priority: 1000,
  create: (connection) => {
    // do stuff
  },
  destroy:(connection) => {
    // do stuff
  }
}

api.connections.addMiddleware(connectionMiddleware)

TaskCallback()

Source:
See:

This callback is displayed as part of the Requester class.

Parameters:
Name Type Description
this.worker Object

The task worker, if this is a pre or post process step.

this.args Object

If this is a queue step, the arguemnts to the task

this.queue Object

The queue to be used / is being used.

TaskMiddleware

Source:
See:
Properties:
Name Type Description
name string

Unique name for the middleware.

global Boolean

Is this middleware applied to all tasks?

priority Number

Module load order. Defaults to api.config.general.defaultMiddlewarePriority.

preProcessor ActionHero~TaskCallback

Called berore the action runs. Has access to all params, before sanitizartion. Can modify the data object for use in actions.

postProcessor ActionHero~TaskCallback

Called after the action runs.

preEnqueue ActionHero~TaskCallback

Called before a task using this middleware is enqueud.

postEnqueue ActionHero~TaskCallback

Called after a task using this middleware is enqueud.

Middleware definition for Actions

Type:
  • Object
Example
api.taskTimer = {
  middleware: {
    name: 'timer',
    global: true,
    priority: 90,
    preProcessor: function(next){
      var worker = this.worker;
      worker.start = process.hrtime();
      next();
    },
    postProcessor: function(next){
      var worker = this.worker;
      var elapsed = process.hrtime(worker.start);
      var seconds = elapsed[0];
      var millis = elapsed[1] / 1000000;
      api.log(worker.job.class + ' done in ' + seconds + ' s and ' + millis + ' ms.', 'info');
      next();
    },
    preEnqueue: function(next){
      var params = this.args[0];
      //Validate params
      next(null, true); //callback is in form cb(error, toRun)
    },
    postEnqueue: function(next){
      api.log("Task successfully enqueued!");
      next();
    }
  }
};

api.tasks.addMiddleware(api.taskTimer.middleware);