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

Request

class

Extends EventEmitter

Request objects are created internally by Serviceberry and passed as the first argument to Handlers. They are wrapper objects around Node's http.IncomingMessage.


  • error HttpError

  • incomingMessage http.IncomingMessage

  • latestResult any

  • remainingPath string

  • fail(error[, status[, headers]])

  • getBody()

  • getBodyParam(name)

  • getCharset()

  • getContent()

  • getContentType()

  • getCurrentPath()

  • getElapsedTime()

  • getEncoding()

  • getFullUrl()

  • getHeader(name)

  • getHeaders()

  • getHost()

  • getId()

  • getIp()

  • getMethod()

  • getPathParam(name)

  • getPathParams()

  • getParams()

  • getParam(name)

  • getPort()

  • getProtocol()

  • getQueryParam(name)

  • getQueryParams()

  • getUrl()

  • hasHeader(name)

  • pipe(stream[, options])

  • proceed([result])

  • withoutHeader(name)

Properties

error

Available within coping handlers.

incomingMessage

The preferred way to interact with a request is through it's methods, however if a need arises that is not addressed with an existing method, then direct access to the http.IncomingMessage is available.

latestResult

The resolved result of the latest finished handler.

remainingPath

The portion of the URL pathname that is after the current path of the currently executing handler.

Methods

fail(error[, status[, headers]])

Call this method to pass control of the request the nearest coping handler. Handlers receiving Request may optionally pass control by throwing an exception or by their return value in lieu of calling fail. See handlers guide.

  • error string or error

    Can be a error message or an error object.

  • status number, string or object [optional]

    Default 500

    Can be a status code, well know status text or an object with properties code and text. See HttpError methods setStatusCode, setStatusText, and setStatus.

  • headers object [optional]

    Response headers if fail results in the end of the request. These headers are not set on the response if a coping handler recovers from the fail and the request continues.

getBody()

Returns any (probably an object - determined by the deserializer)

Deserialized request content. Casing of param names is determined by the deserializer. Deserializers are encouraged to preserve case.

getBodyParam(name)

Returns any

The value of a body param.

  • name string

    Case insensitive.

getCharset()

Returns a string

Alias for getEncoding

getContent()

Returns a stream, a buffer, or a string

After decoding (if content has a character set) and buffering (default) - before deserializing. Most likely use case is within a deserializer.

getContentType()

Returns a string

Content mime type without encoding charset, such as application/json.

getCurrentPath()

Returns a string

The URL path of the currently executing handler.

getElapsedTime()

Returns a number

Number of milliseconds since the start of processing the request.

Starts in the Request constructor and is not the same as time since the client sent the request.

getEncoding()

Returns a string

Name of encoding such as utf-8.

getFullUrl()

Returns a string

Full URL including protocol, host, path and query string.

Relies on Host header.

getHeader(name)

Returns a string or array

Value is an array when header is duplicated.

  • name string

    Case insensitive.

getHeaders()

Returns an object

All headers. Names are lower case and values are arrays when names are duplicated.

getHost()

Returns a string

Host header value.

getId()

Returns a string

Unique identifier for the request.

getIp()

Returns a string

Remote IP address of the client making the request.

getMethod()

Returns a string

HTTP method (verb) such as GET, POST, PUT, DELETE...

getPathParam(name)

Returns a string

The value of a path param.

  • name string

getPathParams()

Returns an object

All path params.

getParams()

Returns an object

All params deserialized from the path, the query string and the request body. Will throw an exception if deserializing the params results in an exception.

When params exist in more than one location such as both the path and the query string, the order of priority is path, then query string, then request body. Because of this ambiguity, it is usually preferable to use getPathParms, getQueryParams, or getBody instead of getParams.

getParam(name)

Returns any

Param deserialized from the path, the query string or the request body. Will throw an exception if deserializing the params results in an exception.

When a param exists in more than one location such as both the path and the query string, the order of priority is path, then query string, then request body. Because of this ambiguity, it is usually preferable to use getPathParm, getQueryParam, or getBodyParam instead of getParam.

  • name string

    Case insensitive.

getPort()

Returns a number

Port number of the request.

getProtocol()

Returns a string

Protocol of the request such as http or https. Will be the X-Forwarded-Proto header value when available.

getQueryParam(name)

Returns a string

The value of a query string param.

  • name string

    Case insensitive.

getQueryParams()

Returns an object

All query string params.

getUrl()

Returns an object

Parsed URL object. See url.

hasHeader(name)

Returns a boolean

true when the header is in the request.

  • name string

    Case insensitive.

pipe(stream[, options])

Returns a stream

If Branch options property buffer is false for the request's route then the request content will be a readable stream. Calling pipe() sets the request content to the result of piping the request content stream through a transform stream.

  • stream stream

  • options object [optional]

    See readable stream.

proceed([result])

Call this method to pass control of the request to the next handler. Handlers may optionally pass control by their return value in lieu of calling proceed. See Handlers guide. Proceed is bound to request

  • result any [optional]

    Set as latestResult property with the exception of when a request is being serialized or deserialized. When serializing or deserializing the result is the response content or request body respectively.

withoutHeader(name)

Returns a boolean

true when the header is NOT in the request.

  • name string

    Case insensitive.

← LeafResponse →
  • Properties
    • error
    • incomingMessage
    • latestResult
    • remainingPath
  • Methods
    • fail(error[, status[, headers]])
    • getBody()
    • getBodyParam(name)
    • getCharset()
    • getContent()
    • getContentType()
    • getCurrentPath()
    • getElapsedTime()
    • getEncoding()
    • getFullUrl()
    • getHeader(name)
    • getHeaders()
    • getHost()
    • getId()
    • getIp()
    • getMethod()
    • getPathParam(name)
    • getPathParams()
    • getParams()
    • getParam(name)
    • getPort()
    • getProtocol()
    • getQueryParam(name)
    • getQueryParams()
    • getUrl()
    • hasHeader(name)
    • pipe(stream[, options])
    • proceed([result])
    • withoutHeader(name)
Guides
Getting StartedHandlersPluginsSerializers and DeserializersSerice TreeAuto ResponsesHow it Works
API Reference
ServiceberryTrunkBranchLeafReqeustResponseHttpError
More
BlogGitHubStarContributingLicense