Web Service

The web service creates an express app which can be used by services for setting up web routes for API's, static content, web applications, etc. This allows users to interact with various bitcore node services over one http or https port.

In order for your service to add routes, it must implement the setupRoutes() and getRoutePrefix() methods.

Example

MyService.prototype.setupRoutes = function(app, express) {
  // Set up routes
  app.get('/hello', function(req, res) {
    res.send('world');
  });

  // Serve static content
  app.use('/static', express.static(__dirname + '/static'));
};

MyService.prototype.getRoutePrefix = function() {
  return 'my-service'
};

Configuring Web Service for HTTPS

You can run the web service over https by editing your bitcore node config, setting https to true and adding httpsOptions:

{
  "port": 3001,
  "https": true,
  "httpsOptions": {
    "key": "path-to-private-key",
    "cert": "path-to-certificate"
  },
  "services": [
    "web"
  ]
}

WebService(options)

This service represents a hub for combining several services over a single HTTP port. Services can extend routes by implementing the methods getRoutePrefix and setupRoutes. Additionally events that are exposed via the getPublishEvents and API methods exposed via getAPIMethods will be available over a socket.io connection.

Kind: global function

Param Type Description
options Object
options.node Node A reference to the node
options.https Boolean Enable https, will default to node.https settings.
options.httpsOptions Object Options passed into https.createServer, defaults to node settings.
options.httpsOptions.key String Path to key file
options.httpsOptions.cert String Path to cert file
options.enableSocketRPC Boolean Option to enable/disable websocket RPC handling
options.port Number The port for the service, defaults to node settings.

webService.start(callback)

Called by Node to start the service

Kind: instance method of WebService

Param Type
callback function

webService.stop(callback)

Called by Node. stop the service

Kind: instance method of WebService

Param Type
callback function

webService.setupAllRoutes()

This function will iterate over all of the available services gathering all of the exposed HTTP routes.

Kind: instance method of WebService

webService.createMethodsMap()

This function will construct an API methods map of all of the available methods that can be called from enable services.

Kind: instance method of WebService

webService.getEventNames()

This function will gather all of the available events exposed from the enabled services.

Kind: instance method of WebService

webService.socketHandler(socket)

This function is responsible for managing a socket.io connection, including instantiating a new Bus, subscribing/unsubscribing and handling RPC commands.

Kind: instance method of WebService

Param Type Description
socket Socket A socket.io socket instance

webService.socketMessageHandler(message, socketCallback)

This method will handle incoming RPC messages to a socket.io connection, call the appropriate method, and respond with the result.

Kind: instance method of WebService

Param Type Description
message Object The socket.io "message" object
socketCallback function

webService.transformHttpsOptions()

This method will read key and cert from disk based on httpsOptions and replace the options with the files.

Kind: instance method of WebService