autohost
An express-based http helper lib.
Ripped from the guts of Anvil.
Features
- Resource-based: define protocol-agnostic resources that interact via HTTP or WebSockets
- Auto-reload resource on file change
- WebSocket support (no fallback to other protocols)
- UI dashboard to review resources' routes, topics and hosted paths
UI Dashboard
Simple navigate to /_autohost to review the current set of resources:
Quick Start
var host = require( 'autohost' )();
host.init();
Configuration
Configuration can be provided optionally to the init call or during instantiation after the require. The object literal follows the format:
{
processes: 1,
static: './public',
resources: './resource',
port: 8800,
allowedOrigin: 'leankit.com',
websockets: true
}
Resources
Resources are expected to be simple modules that return a parameterless function resulting in a JS literal that follows the format:
{
name: 'resource-name',
resources: '',
actions: [
{
alias: 'send',
verb: 'get',
topic: 'send',
path: '',
handle: function( envelope ) {
}
}
]
}
API
Lots of methods are available but not generally intended for use outside the library. Other than init, there are two methods for interacting with connected websocket clients.
- sendToClient( id, topic, message ) - sends message to specific client via websocket (returns true if successful)
- notifyClients( topic, message ) - sends message to all clients via web sockets
Events
- 'socket.client.connected', { socket: socketConnection } - raised when a client connects
- 'socket.client.identified', { socket: socketConnection, id: id } - raised when client reports unique id
TO DO
- Add support for clustering (multiple listening processes)
- Add support for basic auth / token auth / custom auth
- Add support for rate limiting
- Better logging
- More thorough error handling
- Support either Socket.IO or WebSockets via config
License
MIT License - http://opensource.org/licenses/MIT