API Docs for: HEAD
Show:

Response Class

Represents a server response.

Constructor

Response

(
  • cb
)

Parameters:

  • cb Function

    Callback to be used when the response is finished. See the end event on the on method documentation.

Item Index

Methods

chain

(
  • response
)
deprecated

Chains the response given as a parameter to the current object. This means that as data is written to the current object, that same data will be written in the parameter (ie. it honours and replicates streaming). It's similar to copyFrom, but with the following differences:

  • chain is called on the source object, not the target.
  • chain is to be used before any data is written to the source object.
  • chain will write data as it's received, instead of everything at once.

Parameters:

  • response Response

    Response object to chain to the current object.

copyFrom

(
  • res
)

Copies the response given as a parameter into the current response object. This is intended to be used when the response given as a parameter is already finished.

Parameters:

end

()

Marks the response as complete and calls the 'end' callback. When called in the response object provided by RoboHydra, this closes the connection.

follow

(
  • response
)

Follows the given response: as data is written to the given response object, that same data will be written in the calling object (ie. it honours and replicates streaming). It's similar to copyFrom, but with the following differences:

  • follow is to be used before any data is written to the source object (ie. the parameter).
  • follow will write data as it is received, instead of everything at once.

Parameters:

  • response Response

    Response object to follow.

forward

(
  • res
)

Forwards the response given as a parameter. That is, copies the given response in the current object, then marks the given object as finished.

Parameters:

  • res Response

    The response object to forward.

on

(
  • eventName
  • cb
)

Adds a callback to the given event. An event can have more than one callback. All callbacks for an event will be called in order when the event is triggered.

The callback function will receive a single parameter, event, an object with the property type set to the event type, plus different properties according to the event fired. It returns the response object.

The list of response object events is:

  • head: Fired when the header is written. Event objects for this event contain two properties, statusCode and headers.
  • data: Fired when there is data written in the response object. Event objects for this event contain a single property, data, an instance of Buffer.
  • end: Fired when the response is finished. Event objects for this event contain a single property, response, the response object that fired the event.

For responses you have created yourself to pass to the next function, the end event would typically be used to inspect or modify the response contents, then write data to the response object you received, possibly with the help of the methods below.

Parameters:

  • eventName String

    The name of the event to attach the callback to. Possible event names are head, data and end.

  • cb Function

    The callback to attach to the given event.

send

(
  • data
)

Appends the given data to the response body and closes it. Equivalent to calling the write method, then end.

Parameters:

  • data Buffer

    to add to the response body.

write

(
  • chunk
)

Appends data to the response body. This method allows a RoboHydra head to write the response body in chunks, and the response will be sent in chunks to the client (so you could, say, send data, then wait, then send more data, wait, then close the connection).

Parameters:

  • chunk Buffer

    Data to add to the current response body. This parameter can be a string, too.

writeHead

(
  • statusCode
  • headers
)

Sets and writes the response headers.

Parameters:

  • statusCode Integer

    The response's status code.

  • headers Object

    The response's headers.

Attributes

body

Buffer

Response body.

ended

Boolean

Whether the response is finished.

headers

Object

Response headers. Note that the header names are Node-style (ie. lowercase).

statusCode

Integer

Response status code (by default, 200).