Serviceberry
  • Guides
  • API Reference
  • Blog

›API Reference

Guides

  • Getting Started
  • Handlers
  • Plugins
  • Serializers and Deserializers
  • Service Tree
  • Auto Responses
  • How It Works

API Reference

  • Serviceberry
  • Trunk
  • Branch
  • Leaf
  • Request
  • Response
  • HttpError

Trunk

class

Extends Branch

A trunk should be created using serviceberry.createTrunk(options). All branches and leaves originate from the trunk. A service has only one trunk.


  • at(path[, options[, ...handlers]])
  • cope(...handlers)
  • leaf()
  • on(options, ...handlers)
  • on(method, ...handlers)
  • start(callback)
  • use(...handlers)
  • waitFor(...setup)

Methods

at(path[, options[, ...handlers]])

Returns a new branch

Routes requests at a path down a new branch.

  • path string

    Paths are cumulative. Creating a new branch only requires the next piece of the path. For example, if a branch's path is "widgets" a new branch created with branch.at("{id}") will recieve a request to "widgets/42".

    Path paramaters are delimited using a pair of curly braces. Such as {id}. The branch will parse the path parameters. They are available to all handlers through request.getPathParam(name) and request.getPathParms().

  • options object [optional]

    • serializers object

      Property names must be content types such as "application/json" and values must be serializer plugins.

    • deserializers object

      Property names must be content types such as "application/json" and values must be deserializer plugins.

    • buffer boolean [optional]

      Default true

      Whether to buffer the request content. If buffer is false, the request content will be a stream instead of a string or buffer.

  • handlers function or object [optional]

    See Handlers guide.

cope(...handlers)

Returns this trunk

Adds error handlers to this trunk.

  • handlers function or object

    See Handlers guide.

leaf()

Returns new leaf

*The use case for this function is rare. Most of the time you probably want .on().

Routes request to a new leaf. Works the same as .on() expect it returns the new leaf instead of this trunk.

on(options, ...handlers)

Returns this trunk

Routes requests to a new leaf. Requests that match the options will be handled by the new leaf's handlers.

  • options object
    • method string or array [optional]

      HTTP methods such as "GET", "POST", "PUT"... If not specified or is "*", all methods are matched. See nodejs.org for a list of supported methods.

    • consumes string or array [optional]

      Request content types such as "application/json", "text/csv"... If not specified all request content types are matched.

    • produces string or array [optional]

      Response content types such as "application/json", "text/csv"... If not specified all response content types are matched.

    • serializers object

      Property names must be content types such as "application/json" and values must be serializer plugins.

    • deserializers object

      Property names must be content types such as "application/json" and values must be deserializer plugins.

    • buffer boolean [optional]

      Default true

      Whether to buffer the request content. If buffer is false, the request content will be a stream instead of a string or buffer.

  • handlers function or object

    See Handlers guide.

on(method, ...handlers)

Returns this trunk

Routes requests to a new leaf. Requests that match the method will be handled by the new leaf's handlers.

  • method string or array

    HTTP methods such as "GET", "POST", "PUT"... "*" matches all methods. See nodejs.org for a list of supported methods.

  • handlers function or object

    See Handlers guide.

start(callback)

Begin listening for requets. If the trunk was created with the option autoStart, start is called automatically.

  • callback function

use(...handlers)

Returns this trunk

Adds plugin handlers to this trunk.

  • handlers function or object

    See Handlers guide.

waitFor(...setup)

Returns this branch

A hook for asynchronous setup tasks. The service won't be started until all asynchronous setup is complete.

  • setup promise
← ServiceberryBranch →
  • Methods
    • at(path[, options[, ...handlers]])
    • cope(...handlers)
    • leaf()
    • on(options, ...handlers)
    • on(method, ...handlers)
    • start(callback)
    • use(...handlers)
    • waitFor(...setup)
Guides
Getting StartedHandlersPluginsSerializers and DeserializersSerice TreeAuto ResponsesHow it Works
API Reference
ServiceberryTrunkBranchLeafReqeustResponseHttpError
More
BlogGitHubStarContributingLicense