Constructor
new Request(init)
Creates a new instance of the Request class.
Parameters:
PropertiesName | Type | Description |
---|---|---|
init | object | Optional. An initial object to populate the request. |
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
init.scheme | string | <optional> | "" | The scheme of the request. |
init.host | string | <optional> | "" | The host of the request. |
init.port | number | <optional> | 0 | The port of the request. |
init.path | string | <optional> | "" | The path of the request. |
init.method | string | <optional> | "" | The method of the request. |
init.queries | string | <optional> | "" | The queries of the request. |
init.headers | Array | <optional> | [] | The headers of the request. |
init.textBody | string | <optional> | null | The text body of the request. |
Example
// Creating a new instance of the Request class with custom values
const request = new Request({
scheme: 'https',
host: 'example.com',
port: 8080,
path: '/api',
method: 'GET',
queries: 'param1=value1¶m2=value2',
headers: [
['Content-Type', 'application/json'],
['Authorization', 'Bearer token']
],
textBody: '{"key": "value"}'
});
console.log(request.encode());
Members
body
Gets or sets request body.
PropertiesName | Type | Description |
---|---|---|
setter | string | | Sets request body with typeof {string | Map | Array | URLSearchParams}. |
getter | Body | Gets request body as type {Body}. |
// Getting the body of the request
console.log(request.body); // Output: Body { ... }
// Setting the body of the request using a string
request.body = 'This is the request body as a string';
console.log(request.body.parseText());
// Setting the body of the request using JSON data
const jsonData = { key: 'value', nested: { foo: 'bar' } };
request.body = JSON.stringify(jsonData);
console.log(request.body.parseText());
// Setting the body of the request using URLSearchParams
const urlSearchParams = new URLSearchParams();
urlSearchParams.append('param1', 'value1');
urlSearchParams.append('param2', 'value2');
request.body = urlSearchParams;
console.log(request.body.parseText());
headers
Gets the headers of the request.
PropertiesName | Type | Description |
---|---|---|
getter | Headers | Gets the headers of the request. |
// Getting the headers of the request
console.log(request.headers); // Output: Headers { ... }
host
Sets or gets the host of the request.
PropertiesName | Type | Description |
---|---|---|
getter | string | Gets the host of the request. |
setter | string | Sets the host of the request. |
// Getting and setting properties of the request
console.log(request.host); // Output: example.com
// Setting the host of the request
request.host = 'example.org';
console.log(request.host); // Output: example.org
method
Gets or sets the method of the request.
PropertiesName | Type | Description |
---|---|---|
getter | string | Gets the method of the request. |
setter | string | Sets the method of the request. |
// Getting the method of the request
console.log(request.method); // Output: GET
// Setting the method of the request
request.method = 'POST';
console.log(request.method); // Output: POST
path
Sets or sets the path of the request.
PropertiesName | Type | Description |
---|---|---|
getter | string | Gets the path of the request. |
setter | string | Sets the path of the request. |
// Getting and setting properties of the request
console.log(request.path); // Output: /api
// Setting the path of the request
request.path = '/new';
console.log(request.path); // Output: /new
port
Sets or gets the port of the request.
PropertiesName | Type | Description |
---|---|---|
getter | number | Gets the port of the request. |
setter | number | Sets the port of the request. |
// Getting and setting properties of the request
console.log(request.port); // Output: 8080
// Setting the port of the request
request.port = 3000;
console.log(request.port); // Output: 3000
queries
#Gets the queries of the request.
PropertiesName | Type | Description |
---|---|---|
getter | URLSearchParams | Gets the queries of the request. |
// Getting the queries of the request
console.log(request.queries); // Output: URLSearchParams { ... }
scheme
Sets the scheme of the request.
PropertiesName | Type | Description |
---|---|---|
getter | string | Gets the scheme of the request. |
setter | string | Sets the scheme of the request. |
// Getting and setting properties of the request
console.log(request.scheme); // Output: https
// Setting the scheme of the request
request.scheme = 'http';
console.log(request.scheme); // Output: http