Response Class
Represents a server response.
Constructor
Response
-
cb
Parameters:
-
cb
FunctionCallback to be used when the response is finished. See the
end
event on theon
method documentation.
Item Index
Attributes
Methods
chain
-
response
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
ResponseResponse 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:
-
res
ResponseResponse object to copy.
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
ResponseResponse 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
ResponseThe 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
andheaders
.data
: Fired when there is data written in the response object. Event objects for this event contain a single property,data
, an instance ofBuffer
.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
StringThe name of the event to attach the callback to. Possible event names are
head
,data
andend
. -
cb
FunctionThe 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
Bufferto 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
BufferData 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
IntegerThe response's status code.
-
headers
ObjectThe response's headers.