ObjectHttpResponse
HttpResponse is an HTTP response object, usingHttpRequest.responseThe object completes the HTTP server data response, orhttp.requestRequest response data returned from the server
The following example shows how tohttp.ServerUsed in, the sample code is as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14const http = require('http');
const server = new http.Server(8080, (request) => {
// retreive the response object
const response = request.response;
// set the status code
response.statusCode = 200;
// set the content type to text/plain
response.setHeader('Content-Type', 'text/plain');
// write the response body
response.write('ok');
});
server.start();
inheritance relationship
Constructor
HttpResponse
HttpResponse constructor, creates a new HttpResponse object
1new HttpResponse();
constant
TEXT
Specify message type 1, which represents a text type
1const HttpResponse.TEXT = 1;
BINARY
Specify message type 2, representing a binary type
1const HttpResponse.BINARY = 2;
member properties
statusCode
Integer, query and set the return status of the response message
1Integer HttpResponse.statusCode;
statusMessage
String, query and set the return message of the response message
1String HttpResponse.statusMessage;
status
Integer, query and set the return status of the response message, equivalent to statusCode
1Integer HttpResponse.status;
ok
Boolean, query whether the current response is normal
1readonly Boolean HttpResponse.ok;
cookies
NArray, returns the current messageHttpCookieobject list
1readonly NArray HttpResponse.cookies;
protocol
String, protocol version information, allowed format is: HTTP/#.#
1String HttpResponse.protocol;
headers
HttpCollection, included in the messagehttpContainer for message headers, read-only property
1readonly HttpCollection HttpResponse.headers;
keepAlive
Boolean, query and set whether to maintain the connection
1Boolean HttpResponse.keepAlive;
upgrade
Boolean, query and set whether it is an upgrade protocol
1Boolean HttpResponse.upgrade;
maxHeadersCount
Integer, query and set the maximum number of request headers, the default is 128
1Integer HttpResponse.maxHeadersCount;
maxHeaderSize
Integer, query and set the maximum request header length, the default is 8192
1Integer HttpResponse.maxHeaderSize;
maxBodySize
Integer, query and set the maximum body size in MB, the default is 64
1Integer HttpResponse.maxBodySize;
socket
Stream, Query the source socket of the current object
1readonly Stream HttpResponse.socket;
value
String, the basic content of the message
1String HttpResponse.value;
params
NArray, the basic parameters of the message
1readonly NArray HttpResponse.params;
type
Integer, message type
1Integer HttpResponse.type;
data
Value, query the data of the message. This attribute will return different data according to the content-type. When it is text, the text will be returned.jsonreturn whenjson, otherwise it returns binary
1readonly Value HttpResponse.data;
body
SeekableStream, a stream object containing the data portion of the message
1SeekableStream HttpResponse.body;
length
Long, the length of the message data part
1readonly Long HttpResponse.length;
stream
Stream, the stream object when querying the message readFrom
1readonly Stream HttpResponse.stream;
lastError
String, query and set the last error of message processing
1String HttpResponse.lastError;
member function
writeHead
Set the return status of the response message, return the message, and add the response header
1
2
3HttpResponse.writeHead(Integer statusCode,
String statusMessage,
Object headers = {});
Call parameters:
- statusCode: Integer, specifies the return status of the response message
- statusMessage: String, specifies the return message of the response message
- headers: Object, specifies the response header added to the response message
Set the return status of the response message, return the message, and add the response header
1
2HttpResponse.writeHead(Integer statusCode,
Object headers = {});
Call parameters:
- statusCode: Integer, specifies the return status of the response message
- headers: Object, specifies the response header added to the response message
addCookie
Add one to cookiesHttpCookieobject
1HttpResponse.addCookie(HttpCookie cookie);
Call parameters:
- cookie:HttpCookie, specify theHttpCookieobject
redirect
Send redirect to client
1HttpResponse.redirect(String url);
Call parameters:
- url: String, redirected address
Send redirect to client
1
2HttpResponse.redirect(Integer statusCode,
String url);
Call parameters:
- statusCode: Integer, specifies the return status of the response message, the accepted status is: 301, 302, 307
- url: String, redirected address
sendHeader
Send formatted onlyhttpHead to the given stream object
1HttpResponse.sendHeader(Stream stm) async;
Call parameters:
- stm:Stream, specifies the stream object that receives the formatted message
hasHeader
Check whether there is a message header with the specified key value
1Boolean HttpResponse.hasHeader(String name);
Call parameters:
- name: String, specifies the key value to be checked
Return results:
- Boolean, returns whether the key value exists
firstHeader
Query the first message header of the specified key value
1String HttpResponse.firstHeader(String name);
Call parameters:
- name: String, specify the key value to be queried
Return results:
- String, returns the value corresponding to the key value, if it does not exist, returns undefined
allHeader
Query all message headers of the specified key value
1NObject HttpResponse.allHeader(String name = "");
Call parameters:
- name: String, specify the key value to be queried, pass an empty string to return the results of all key values
Return results:
- NObject, returns an array of all values corresponding to the key value. If the data does not exist, returns null.
addHeader
Add a message header, add data and do not modify the message header of the existing key value
1HttpResponse.addHeader(Object map);
Call parameters:
- map: Object, specifies the key-value data dictionary to be added
Adds a set of headers with the specified name. Adding data does not modify the headers of existing key values.
1
2HttpResponse.addHeader(String name,
Array values);
Call parameters:
- name: String, specifies the key value to be added
- values: Array, specifies a set of data to be added
Add a message header, add data and do not modify the message header of the existing key value
1
2HttpResponse.addHeader(String name,
String value);
Call parameters:
- name: String, specifies the key value to be added
- value: String, specify the data to be added
setHeader
Set a message header. Setting the data will modify the first value corresponding to the key value and clear the remaining message headers with the same key value.
1HttpResponse.setHeader(Object map);
Call parameters:
- map: Object, specifies the key-value data dictionary to be set
Set a set of message headers with the specified name. Setting the data will modify the value corresponding to the key value and clear the remaining message headers with the same key value.
1
2HttpResponse.setHeader(String name,
Array values);
Call parameters:
- name: String, specify the key value to be set
- values: Array, specifies a set of data to be set
Set a message header. Setting the data will modify the first value corresponding to the key value and clear the remaining message headers with the same key value.
1
2HttpResponse.setHeader(String name,
String value);
Call parameters:
- name: String, specify the key value to be set
- value: String, specify the data to be set
removeHeader
Delete all message headers with specified key value
1HttpResponse.removeHeader(String name);
Call parameters:
- name: String, specifies the key value to be deleted
read
Read data of specified size from the stream. This method is an alias of the corresponding method of body.
1Buffer HttpResponse.read(Integer bytes = -1) async;
Call parameters:
- bytes: Integer, specifies the amount of data to be read. The default is to read data blocks of random size. The size of the data read depends on the device.
Return results:
- Buffer, returns the data read from the stream. If there is no data to read or the connection is interrupted, null is returned.
readAll
Read all remaining data from the stream. This method is an alias of the corresponding method of body.
1Buffer HttpResponse.readAll() async;
Return results:
- Buffer, returns the data read from the stream. If there is no data to read or the connection is interrupted, null is returned.
write
Write the given data. This method is an alias of the corresponding method in body.
1HttpResponse.write(Buffer data) async;
Call parameters:
- data:Buffer, given the data to be written
json
Writes the given data in JSON encoding
1Value HttpResponse.json(Value data);
Call parameters:
- data: Value, given the data to be written
Return results:
- Value, this method does not return data
Parse the data in the message as JSON encoding
1Value HttpResponse.json();
Return results:
- Value, returns the parsed result
pack
bymsgpackEncoding writes the given data
1Value HttpResponse.pack(Value data);
Call parameters:
- data: Value, given the data to be written
Return results:
- Value, this method does not return data
bymsgpackEncoding and parsing the data in the message
1Value HttpResponse.pack();
Return results:
- Value, returns the parsed result
end
Set the end of current message processing,ChainThe processor does not continue with subsequent transactions
1HttpResponse.end();
isEnded
Query whether the current message has ended
1Boolean HttpResponse.isEnded();
Return results:
- Boolean, returns true when finished
clear
Clear message content
1HttpResponse.clear();
sendTo
Sends a formatted message to the given stream object
1HttpResponse.sendTo(Stream stm) async;
Call parameters:
- stm:Stream, specifies the stream object that receives the formatted message
readFrom
Reads the formatted message from the given cache stream object and parses the populated object
1HttpResponse.readFrom(Stream stm) async;
Call parameters:
- stm:Stream, specifies the stream object for reading formatted messages
toString
Returns the string representation of the object. Generally, "[Native Object]" is returned. The object can be re-implemented according to its own characteristics.
1String HttpResponse.toString();
Return results:
- String, returns the string representation of the object
toJSON
Returns a JSON format representation of the object, generally returning a collection of readable properties defined by the object.
1Value HttpResponse.toJSON(String key = "");
Call parameters:
- key: String, not used
Return results:
- Value, returns a value containing JSON serializable