This object is created internally by an HTTP server — not by the user. It is passed as the second parameter to the 'request' event.
Variables
sendDate:Bool
When true, the Date header will be automatically generated and sent in the response if it is not already present in the headers. Defaults to true.
This should only be disabled for testing; HTTP requires the Date header in responses.
read onlysocket:Socket
Reference to the underlying socket. Usually users will not want to access this property.
In particular, the socket will not emit 'readable'
events because of how the protocol parser attaches to the socket.
After end()
, the property is nulled. The socket
may also be accessed via connection
.
statusCode:Int
When using implicit headers (not calling writeHead
explicitly), this property controls the status code
that will be sent to the client when the headers get flushed.
After response header was sent to the client, this property indicates the status code which was sent out.
statusMessage:String
When using implicit headers (not calling writeHead()
explicitly),
this property controls the status message that will be sent to the client when the headers get flushed.
If this is left as undefined
then the standard message for the status code will be used.
After response header was sent to the client, this property indicates the status message which was sent out.
Methods
addTrailers(headers:DynamicAccess<String>):Void
addTrailers(headers:Array<Array<String>>):Void
This method adds HTTP trailing headers (a header but at the end of the message) to the response.
Trailers will only be emitted if chunked encoding is used for the response; if it is not (e.g., if the request was HTTP/1.0), they will be silently discarded.
Note that HTTP requires the 'Trailer' header to be sent if you intend to emit trailers, with a list of the header fields in its value.
getHeader(name:String):EitherType<String, Array<String>>
Reads out a header that's already been queued but not sent to the client.
The name is case-insensitive. The type of the return value depends on the arguments provided to setHeader()
.
getHeaderNames():Array<String>
Returns an array containing the unique names of the current outgoing headers. All header names are lowercase.
getHeaders():DynamicAccess<EitherType<String, Array<String>>>
Returns a shallow copy of the current outgoing headers. Since a shallow copy is used, array values may be mutated without additional calls to various header-related http module methods. The keys of the returned object are the header names and the values are the respective header values. All header names are lowercase.
The object returned by the getHeaders()
method does not prototypically inherit from the JavaScript Object.
This means that typical Object
methods such as obj.toString()
, obj.hasOwnProperty()
, and others are not defined and will not work.
hasHeader(name:String):Bool
Returns true if the header identified by name
is currently set in the outgoing headers.
The header name matching is case-insensitive.
setHeader(name:String, value:String):Void
setHeader(name:String, value:Array<String>):Void
Sets a single header value for implicit headers.
If this header already exists in the to-be-sent headers, its value will be replaced.
Use an array of strings here to send multiple headers with the same name.
Non-string values will be stored without modification.
Therefore, getHeader()
may return non-string values.
However, the non-string values will be converted to strings for network transmission.
setTimeout(msecs:Int, ?callback:() ‑> Void):Void
Sets the Socket's timeout value to msecs
.
If a callback is provided, then it is added as a listener on the 'timeout'
event on the response object.
If no 'timeout'
listener is added to the request, the response, or the server, then sockets are destroyed when they time out.
If a handler is assigned to the request, the response, or the server's 'timeout'
events, timed out sockets must be handled explicitly.
writeContinue():Void
Sends a HTTP/1.1 100 Continue message to the client, indicating that the request body should be sent.
See the 'checkContinue'
event on Server
.
writeHead(statusCode:Int, reasonPhrase:String, ?headers:DynamicAccess<String>):Void
writeHead(statusCode:Int, ?headers:DynamicAccess<String>):Void
Sends a response header to the request.
The status code is a 3-digit HTTP status code, like 404
. The last argument, headers
, are the response headers.
Optionally one can give a human-readable statusMessage
as the second argument.
This method must only be called once on a message and it must be called before end()
is called.
If write()
or end()
are called before calling this, the implicit/mutable headers will be calculated and call this function.
When headers have been set with setHeader()
, they will be merged with any headers passed to writeHead()
, with the headers passed to writeHead()
given precedence.
If this method is called and setHeader()
has not been called, it will directly write the supplied header values onto the network channel without caching internally,
and the getHeader()
on the header will not yield the expected result.
If progressive population of headers is desired with potential future retrieval and modification, use setHeader()
instead.
Content-Length
is given in bytes not characters.
The above example works because the string 'hello world'
contains only single byte characters.
If the body contains higher coded characters then Buffer.byteLength()
should be used to determine the number of bytes in a given encoding.
And Node.js does not check whether Content-Length
and the length of the body which has been transmitted are equal or not.
Attempting to set a header field name or value that contains invalid characters will result in a TypeError
being thrown.
writeProcessing():Void
Sends a HTTP/1.1 102 Processing message to the client, indicating that the request body should be sent.