Below is a breakdown of the Response
component which is an extended Writable
stream matching official Node.js network specification.
- See
> [ExpressJS]
for more information on additional compatibility methods and properties. - See
> [Stream.Writable]
for more information on additional native methods and properties.
Property | Type | Description |
---|---|---|
app |
HyperExpress.Server |
HyperExpress Server instance this Response originated from. |
raw |
uWS.Response |
Underlying uWebsockets.js response object. |
sse |
undefined , SSEventStream |
Returns a "Server-Sent Events" connection object for SSE functionality. |
initiated |
Boolean |
Signifies whether the response has been initiated and the status code/headers have been sent. |
aborted |
Boolean |
Signifies whether the request has been aborted/completed. |
completed |
Boolean |
Alias of aborted property. |
write_offset |
Number |
Returns the current response body content write offset in bytes. |
- See
> [SSEventStream]
for more information on theResponse.sse
property for working with Server-Sent Events.
atomic(Function: callback)
: Alias of uWebsockets'scork(callback)
method.- Usage: Wrapping multiple response method calls inside this method can improve performance.
status(Number: code, String?: message)
: Sets the HTTP response status code and message for current request.type(String: mime_type)
: Writes correct protocolcontent-type
header for specified mime type.- Example:
response.type('json')
writesapplication/json
- Supported: Mime Types
- Example:
header(String: name, String|Array<String>: value, Boolean?: overwrite)
: Writes one or multiple response headers.- Note! this method will overwrite any previous values for a header by default.
cookie(String: name, String?: value, Number?: expiry, Object?: options, Boolean: sign_cookie)
: Writes a cookie header to set cookie on response.expiry
specifies the cookie lifetime duration in milliseconds.sign_cookie
istrue
by default.options
:domain
[String
]: Cookie Domainpath
[String
]: Cookie PathmaxAge
[Number
]: Max Cookie Age (In Seconds)secure
[Boolean
]: Adds Secure FlaghttpOnly
[Boolean
]: Adds httpOnly FlagsameSite
[Boolean
,'none'
,'lax'
,'strict'
]: Cookie Same-Site Preferencesecret
:[String
]: Cryptographically signs cookie value
- Note cookie values are not URL encoded.
- Note You may pass
null
as thevalue
parameter to delete a cookie.
upgrade(Object?: context)
: Upgrades incoming request to a WebSocket or Server-Sent Events connection.- Note
context
is optional and can be used to store data for the future. - Note this method can only be used inside an
upgrade
route handler.
- Note
redirect(String: url)
: Writes 302 header to redirect incoming request to specified url.write(String|Buffer|ArrayBuffer: chunk, String?: encoding, Function?: callback)
: Writes specified chunk using chunked transfer. Use this method to stream large amounts of data.- Returns a
Boolean
in whichfalse
signifies chunk was not fully sent due to built up backpressure. - Note the
send()
must still be called in the end after writing all chunks to end the chunked transfer. - Note this method mimics
Writable.write()
method thus you may use direct piping by piping from aReadable
stream.
- Returns a
drain(Function: handler)
: Binds a one-time handler which is called once the built up backpressure from a failedwrite()
call has been drained.- Handle Example:
(Number: offset) => boolean
- Proper Usage:
- You MUST retry the failed chunk
write()
call with the same sliced chunk from before proceeding to writing future chunks. - You should slice the chunk using
chunk.slice(offset - Response.write_offset)
to retry the chunk with thewrite()
call. - This handler may be called multiple times with different
offset
values until the chunk is fully written.
- You MUST retry the failed chunk
- Note this handler must be synchronous only.
- Handle Example:
stream(ReadableStream: readable, Number?: total_size)
: Pipes the provided readable stream as body and sends response.- This method can be useful for serving large amounts of data through Node.js streaming functionalities.
- Note the
total_size
is an optional number inbytes
which can be specified if you need acontent-length
header on the receiver side. - Note you must do your own error handling on the readable stream to prevent triggering the global error handler.
send(String|Buffer|ArrayBuffer?: body)
: Writes specified body and sends response.- Returns a
Boolean
in whichfalse
signifies body was not fully sent due to built up backpressure.
- Returns a
json(Object: body)
: Alias ofsend()
. Sets mime type tojson
and sends response.jsonp(Object: body, String?: name)
: Alias ofsend()
. Sets mime type tojs
and sends response.- Note! This method uses
callback
query parameter as callback name by default ifname
parameter is not specified.
- Note! This method uses
html(String: body)
: Alias ofsend()
. Sets mime type tohtml
and sends response.attachment(String: path)
: Writes appropriateContent-Disposition
andContent-Type
headers for file specified atpath
.- Note! this method only writes the appropriate headers.
download(String: path, String: filename)
: Alias ofsend()
. Sets appropriate attachment headers and mime type if one has not been set yet and sends file content at specified path as response body for browser to download.throw(Error: error)
: Calls the global catch-all error handler (If one is assigned) with the provided error.- See ExpressJS documentation for more properties/methods that are also implemented for compatibility.
The Response
component extends an EventEmitter
/Writable
stream meaning your application can listen for the following lifecycle events.
- [
abort
]: This event will get emitted when the request was aborted unexpectedly by the client or the underlying connection was closed. - [
prepare
]: This event will get emitted when the response is internally ready to be sent. Use this to perform any last minute modifications to the response. - [
finish
]: This event will get emitted when the response has been sent by HyperExpress. This does not mean the client has received anything yet. - [
close
]: This event will get emitted when the underlying connection has closed. - Note! you should utilize the [
close
] event to detect the absolute end of a request as it signifies connection closure. - See the official
> [stream.Writable]
Node.js documentation for more information.