Response
class
Extends EventEmitter
This object is created internally by Serviceberry and passed as the second argument to Handlers.
It is a wrapper object around Node's http.ServerResponse
.
- clearHeaders()
- getBody()
- getContent()
- getContentType()
- getEncoding()
- getHeader(name)
- getHeaders()
- getStatus()
- getStatusCode()
- getStatusText()
- hasHeader(name)
- is(status)
- removeHeader(name)
- send([options])
- setBody(body)
- setContent(content)
- setEncoding(encoding)
- setHeader(name, value)
- setHeaders(headers)
- setStatus(status)
- setStatusCode(code)
- setStatusText(text)
- withoutHeader(name)
Methods
clearHeaders()
Removes all headers.
getBody()
Returns any
The body prior to serialization.
getContent()
Returns string, buffer, or readable stream
The body after serialization.
getContentType()
Returns a string
Content mime type without encoding charset, such as application/json
.
getEncoding()
Returns a string
Name of encoding such as utf-8
.
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.
getStatus()
Returns an object
Status code and text if known. May have code
and/or text
. May be an empty object.
getStatusCode()
Returns a number or undefined
getStatusText()
Returns a string or undefined
hasHeader(name)
Returns a boolean
true
when the header is in the request.
name string
Case insensitive.
is(status)
Returns a boolean
true
when status matches.
status string or number
Status code or standard status text to test against. Case insensitive.
removeHeader(name)
Causes a header not to be sent. Causes hasHeader
method to return false
.
name string
Case insensitive.
send([options])
Sends a response back to the client - ending the request. A response can also be implicity sent when the request proceeds and there are no more handlers in the queue.
options object [optional]
Sets response options before finishing request and sending response.
status number, string or object [optional]
Default 200
Can be a status code, standard status text or an object with properties
code
andtext
. See methodssetStatusCode
,setStatusText
, andsetStatus
.headers object [optional]
body any [optional]
content string, buffer, or readable stream [optional]
Mutually exclusive with
body
option. Content will not be serialized.encoding string [optional]
Default utf8
finish function [optional]
Listener for
http.ServerResponse
'sfinish
event.
setBody(body)
The body prior to serialization.
- body any
setContent(content)
Set the content to bypass serialization.
- content string, buffer, or readable stream
setEncoding(encoding)
Name of encoding such as utf-8
.
- encoding string
setHeader(name, value)
name string
value string, number or array
setHeaders(headers)
Sets headers from an object. Does NOT clear headers. Does override headers.
- headers object
setStatus(status)
When code or text is not provided, an attempt will be made to derive one from
the other. For example setStatus(200)
will set status code to 200
and
status text to OK
and setStatus("OK")
will set status text to OK
and
status code to 200
.
- status number, string or object
code number
text string
setStatusCode(code)
- code number
setStatusText(text)
- text string
withoutHeader(name)
Returns a boolean
true
when the header is NOT in the request.
name string
Case insensitive.