Constructor
new Response(init)
Creates a new instance of the Response class.
Parameters:
PropertiesName | Type | Description |
---|---|---|
init | object | Optional. An initial object to populate the response. |
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
init.statusCode | number | <optional> | 200 | The status code of the response. |
init.statusPhrase | string | <optional> | "OK" | The status phrase of the response. |
init.headers | object | <optional> | {} | The headers of the response. |
init.textBody | string | <optional> | null | The text body of the response. |
Example
// Creating a new instance of the Response class with custom values
const response = new Response({
statusCode: 404,
statusPhrase: 'Not Found',
headers: {
'Content-Type': 'application/json',
'Cache-Control': 'no-cache'
},
textBody: '{"error": "Resource not found"}'
});
console.log(response.encode());
Members
body
Gets or sets the body of the response.
PropertiesName | Type | Description |
---|---|---|
getter | Body | Gets response body as type {Body}. |
setter | string | | Sets response body with typeof {string | Map | Array | URLSearchParams}. |
// Getting the body of the response
console.log(response.body); // Output: Body { ... }
// Setting the body of the response using a string
response.body = 'This is the response body as a string';
console.log(response.body.parseText());
// Setting the body of the response using JSON data
const jsonData = { message: 'Response JSON data' };
response.body = JSON.stringify(jsonData);
console.log(response.body.parseText());
// Setting the body of the response using URLSearchParams
const urlSearchParams = new URLSearchParams();
urlSearchParams.append('param1', 'value1');
urlSearchParams.append('param2', 'value2');
response.body = urlSearchParams;
console.log(response.body.parseText());
headers
Gets the headers of the response.
PropertiesName | Type | Description |
---|---|---|
getter | Headers | Gets the headers of the response. |
// Getting the headers of the response
console.log(response.headers); // Output: Headers { ... }
statusCode
Sets or gets the status code of the response.
PropertiesName | Type | Description |
---|---|---|
getter | number | Gets the status code of the response. |
setter | number | Sets the status code of the response. |
// Getting the status code of the response
console.log(response.statusCode); // Output: 40
// Setting the status code of the response
response.statusCode = 500;
console.log(response.statusCode); // Output: 500
statusPhrase
Sets the status phrase of the response.
PropertiesName | Type | Description |
---|---|---|
getter | string | Gets the status phrase of the response. |
setter | string | Sets the status phrase of the response. |
// Getting the status phrase of the response
console.log(response.statusPhrase); // Output: Not Found
// Setting the status phrase of the response
response.statusPhrase = 'Internal Server Error';
console.log(response.statusPhrase); // Output: Internal Server Error