Skip to content

Commit 80b7a72

Browse files
committed
add node:http and node:https modules
1 parent 5a83835 commit 80b7a72

File tree

2 files changed

+195
-0
lines changed

2 files changed

+195
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
---
2+
pcx_content_type: configuration
3+
title: http
4+
---
5+
6+
import { Render } from "~/components"
7+
8+
<Render file="nodejs-compat-howto" />
9+
10+
## Agent
11+
12+
An Agent manages HTTP connection reuse by maintaining request queues per host/port. In the
13+
workers environment, however, such low-level management of the network connection, ports,
14+
etc, is not relevant because it is handled by the Cloudflare infrastructure instead. Accordingly, the
15+
implementation of `Agent` in Workers is a stub implementation that does not support connection
16+
pooling or keep-alive.
17+
18+
```js
19+
import { Agent } from 'node:http';
20+
import { strictEqual } from 'node:assert/strict';
21+
22+
const agent = new Agent();
23+
strictEqual(agent.protocol, 'http:');
24+
```
25+
26+
## get
27+
28+
The `get` method performs a GET request to the specified URL and invokes the callback with the response. It's a convenience method that simplifies making HTTP GET requests without manually configuring request options.
29+
30+
```js
31+
import { get } from 'node:http';
32+
import { strictEqual, ok } from 'node:assert/strict';
33+
34+
get('http://docs.cloudflare.com/robots.txt', (res) => {
35+
// http://docs.cloudflare.com redirects to https url.
36+
strictEqual(res.statusCode, 301);
37+
let data = '';
38+
res.on('data', (chunk) => {
39+
data += chunk.toString();
40+
});
41+
res.on('end', () => {
42+
// Returns the response body as a string.
43+
console.log(data);
44+
ok(data.includes('301 Moved Permanently'));
45+
});
46+
});
47+
```
48+
49+
## request
50+
51+
The `request` method creates an HTTP request with customizable options like method, headers, and body. It provides full control over the request configuration and returns a [writable stream](https://nodejs.org/docs/latest/api/stream.html#class-streamwritable) for sending request data.
52+
53+
```js
54+
import { request } from 'node:http';
55+
import { strictEqual } from 'node:assert/strict';
56+
57+
const req = request({
58+
method: 'GET',
59+
protocol: 'http:',
60+
hostname: 'docs.cloudflare.com',
61+
path: '/'
62+
}, (res) => {
63+
// http://docs.cloudflare.com redirects to https url.
64+
strictEqual(res.statusCode, 301);
65+
66+
let data = '';
67+
res.on('data', (chunk) => {
68+
data += chunk.toString();
69+
});
70+
res.on('end', () => {
71+
// Returns the response body as a string.
72+
console.log(data);
73+
ok(data.includes('301 Moved Permanently'));
74+
});
75+
});
76+
req.end();
77+
78+
const req2 = request(new URL('http://docs.cloudflare.com'),{
79+
method: 'GET',
80+
}, (res) => {
81+
// http://docs.cloudflare.com redirects to https url.
82+
strictEqual(res.statusCode, 301);
83+
});
84+
85+
req2.end();
86+
```
87+
88+
The following options passed to the `request` method are not supported:
89+
- `maxHeaderSize`
90+
- `insecureHTTPParser`
91+
- `createConnection`
92+
- `lookup`
93+
- `socketPath`
94+
95+
## OutgoingMessage
96+
97+
The [`OutgoingMessage`](https://nodejs.org/docs/latest/api/http.html#class-httpoutgoingmessage) class represents an HTTP response that is sent to the client. It provides methods for writing response headers and body, as well as for ending the response. `OutgoingMessage` extends from the [`Writable` stream class](https://nodejs.org/docs/latest/api/stream.html#class-streamwritable).
98+
99+
```js
100+
import { OutgoingMessage } from 'node:http';
101+
102+
const res = new OutgoingMessage();
103+
res.writeHead(200, { 'Content-Type': 'text/plain' });
104+
res.write('Hello, World!');
105+
res.end();
106+
```
107+
## IncomingMessage
108+
109+
The `IncomingMessage` class represents an HTTP request that is received from the client. It provides methods for reading request headers and body, as well as for ending the request. `IncomingMessage` extends from the `Readable` stream class.
110+
111+
```js
112+
import { get, IncomingMessage } from 'node:http';
113+
import { ok, strictEqual } from 'node:assert/strict';
114+
115+
get('http://docs.cloudflare.com', (res) => {
116+
strictEqual(res.statusCode, 301);
117+
ok(res instanceof IncomingMessage);
118+
});
119+
```
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
---
2+
pcx_content_type: configuration
3+
title: https
4+
---
5+
6+
import { Render } from "~/components"
7+
8+
<Render file="nodejs-compat-howto" />
9+
10+
## Agent
11+
12+
An Agent manages HTTPS connection reuse by maintaining request queues per host/port. The implementation is a stub implementation that does not support connection pooling or keep-alive.
13+
14+
```js
15+
import { Agent } from 'node:https';
16+
import { strictEqual } from 'node:assert/strict';
17+
18+
const agent = new Agent();
19+
strictEqual(agent.protocol, 'https:');
20+
```
21+
22+
## get
23+
24+
The [`get`](https://nodejs.org/docs/latest/api/https.html#httpsgetoptions-callback) method performs a GET request to the specified URL and invokes the callback with the response. It's a convenience method that simplifies making HTTPS GET requests without manually configuring request options.
25+
26+
`get` methods behaves the same as `node:http` get method.
27+
28+
```js
29+
import { get } from 'node:https';
30+
import { strictEqual, ok } from 'node:assert/strict';
31+
32+
get('https://developers.cloudflare.com/robots.txt', (res) => {
33+
strictEqual(res.statusCode, 200);
34+
let data = '';
35+
res.on('data', (chunk) => {
36+
data += chunk.toString();
37+
});
38+
res.on('end', () => {
39+
// Returns the response body as a string.
40+
console.log(data);
41+
ok(data.includes('User-agent'));
42+
});
43+
});
44+
```
45+
46+
## request
47+
48+
The [`request`](https://nodejs.org/docs/latest/api/https.html#httpsrequesturl-options-callback) method creates an HTTPS request with customizable options like method, headers, and body. It provides full control over the request configuration and returns a [writable stream](https://nodejs.org/docs/latest/api/stream.html#class-streamwritable) for sending request data.
49+
50+
Request method accepts all options from `http.request` with some differences in default values:
51+
- `protocol`: default `https:`
52+
- `port`: default `443`
53+
- `agent`: default `https.globalAgent`
54+
55+
The following additional options are not supported: ca, cert, ciphers, clientCertEngine (deprecated), crl, dhparam, ecdhCurve, honorCipherOrder, key, passphrase, pfx, rejectUnauthorized, secureOptions, secureProtocol, servername, sessionIdContext, highWaterMark.
56+
57+
```js
58+
import { request } from 'node:https';
59+
import { strictEqual, ok } from 'node:assert/strict';
60+
61+
const req = request('https://developers.cloudflare.com/robots.txt', {
62+
method: 'GET',
63+
}, (res) => {
64+
strictEqual(res.statusCode, 200);
65+
let data = '';
66+
res.on('data', (chunk) => {
67+
data += chunk.toString();
68+
});
69+
res.on('end', () => {
70+
// Returns the response body as a string.
71+
console.log(data);
72+
ok(data.includes('User-agent'));
73+
});
74+
});
75+
req.end();
76+
```

0 commit comments

Comments
 (0)