Server

ActionHero. Server

new Server()

Source:
Tutorials:
Properties:
Name Type Description
type string

The name & type of the server.

verbs Array

What connection verbs can connections of this type use?

config Object

Shorthand for api.config.servers[this.type]

canChat Boolean

Can connections of this server use the chat system?

logConnections Boolean

Should we log every new connection?

logExits Boolean

Should we log when a connection disconnects/exits?

sendWelcomeMessage Boolean

Should every new connection of this server type recieve the wecome message (defiend in locales, actionhero.welcomeMessage)

Create a new ActionHero Server. The required properties of an server. These can be defined statically (this.name) or as methods which return a value.

Example
const {Server, api} = require('actionhero')

module.exports = class MyServer extends Server {
  constructor () {
    super()
    this.type = '%%name%%'

    this.attributes = {
      canChat: false,
      logConnections: true,
      logExits: true,
      sendWelcomeMessage: false,
      verbs: []
    }
    // this.config will be set to equal api.config.servers[this.type]
  }

  initialize () {
    this.on('connection', (conection) => {

    })

    this.on('actionComplete', (data) => {

    })
  }

  start () {
    // this.buildConnection (data)
    // this.processAction (connection)
    // this.processFile (connection)
  }

  stop () {

  }

  sendMessage (connection, message, messageCount) {

  }

  sendFile (connection, error, fileStream, mime, length, lastModified) {

  }

  goodbye (connection) {

  }
}

Methods

(static) buildConnection()

Source:

Build a the ActionHero.Connection from the raw parts provided by the server.

Example
// from the web server

this.buildConnection({
  rawConnection: {
    req: req,
    res: res,
    params: {},
    method: method,
    cookies: cookies,
    responseHeaders: responseHeaders,
    responseHttpCode: responseHttpCode,
    parsedURL: parsedURL
  },
  id: fingerprint + '-' + uuid.v4(),
  fingerprint: fingerprint,
  remoteAddress: remoteIP,
  remotePort: remotePort
})
Fires:

(static) connections() → {Array}

Source:

Enumerate the connections for this server type on this server.

Returns:

An array of ActionHero.Connection objects

Type
Array

(async, static) initialize()

Source:

Method run as part of the initialize lifecycle of your server. Ususally configures the server.

(static) log(message, severity, data)

Source:

Log a message from this server type. A wrapper around api.log with a server prefix.

Parameters:
Name Type Description
message string

The message to log.

severity string

The severity of the message, ie: 'debug', 'info', 'warning', etc (default 'info').

data Object

Any metadata to add to the log message.

(async, static) processAction(connection) → {Promise}

Source:

When a connection has called an Action command, and all properties are set. Connection should have params.action set at least.

Parameters:
Name Type Description
connection Object

The Connection.

Fires:
Returns:
Type
Promise

(async, static) processFile(connection) → {Promise}

Source:

When a connection has called an File command, and all properties are set. Connection should have params.file set at least. Will eventuall call ActionHero.Server#sendFile.

Parameters:
Name Type Description
connection Object

The Connection.

Returns:
Type
Promise

(async, static) sendFile(connection, error, fileStream, mime, length, lastModified)

Source:

Must be defined explaining how to send a file to an induvidual connection. Might be a noop for some connection types.

Parameters:
Name Type Description
connection Object

The connection in question.

error Error

If there was any errror finding or reading this file.

fileStream Object

A stream from the file reader which can be piped to the connection.

mime string

The mime type of the files

length Number

The length in bytes of the file. Useful for setting headers.

lastModified Date

The timestamp when the file was last modifeid on disk. Useful for headers.

(async, static) sendMessage(connection, message, messageCount)

Source:

Must be defined explaining how to send a message to an induvidual connection.

Parameters:
Name Type Description
connection Object

The connection in question.

message Object

The message to send. May be an Object, Array, or String.

messageCount Object

A count of which message this is to be sent to the client.

(async, static) start()

Source:

Method run as part of the start lifecycle of your server. Ususally boots the server (listens on port, etc).

(async, static) stop()

Source:

Method run as part of the stop lifecycle of your server. Ususally configures the server (disconnects from port, etc).

Events

actionComplete

Source:
See:
Properties:
Name Type Description
data object

The same data from the Action. Includes the connection, response, etc.

Event called when a an action is complete for a connection created by this server. You may want to send a response to the client as a response to this event.

Type:
  • object

connection

Source:

Event called when a formal new connection is created for this server type. This is a resposne to calling ActionHero.Server#buildConnection

Type:
  • object