Skip to content

Commit 6ddc2a0

Browse files
committed
add node:http and node:https modules
1 parent 5a83835 commit 6ddc2a0

File tree

2 files changed

+203
-0
lines changed

2 files changed

+203
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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';
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';
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.setEncoding('utf8');
39+
res.on('data', (chunk) => {
40+
data += chunk;
41+
});
42+
res.on('end', () => {
43+
// Returns the response body as a string.
44+
console.log(data);
45+
ok(data.includes('301 Moved Permanently'));
46+
});
47+
});
48+
```
49+
50+
## request
51+
52+
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.
53+
54+
```js
55+
import { request } from 'node:http';
56+
import { strictEqual } from 'node:assert';
57+
58+
const req = request({
59+
method: 'GET',
60+
protocol: 'http:',
61+
hostname: 'docs.cloudflare.com',
62+
path: '/'
63+
}, (res) => {
64+
// http://docs.cloudflare.com redirects to https url.
65+
strictEqual(res.statusCode, 301);
66+
67+
let data = '';
68+
res.setEncoding('utf8');
69+
res.on('data', (chunk) => {
70+
data += chunk;
71+
});
72+
res.on('end', () => {
73+
// Returns the response body as a string.
74+
console.log(data);
75+
ok(data.includes('301 Moved Permanently'));
76+
});
77+
});
78+
req.end();
79+
80+
const req2 = request(new URL('http://docs.cloudflare.com'),{
81+
method: 'GET',
82+
}, (res) => {
83+
// http://docs.cloudflare.com redirects to https url.
84+
strictEqual(res.statusCode, 301);
85+
});
86+
87+
req2.end();
88+
```
89+
90+
The following options passed to the `request` method are not supported due to the differences in the Cloudflare Workers and the implementation of the node:http module:
91+
- `maxHeaderSize`
92+
- `insecureHTTPParser`
93+
- `createConnection`
94+
- `lookup`
95+
- `socketPath`
96+
97+
## OutgoingMessage
98+
99+
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).
100+
101+
```js
102+
import { OutgoingMessage } from 'node:http';
103+
104+
const res = new OutgoingMessage();
105+
res.writeHead(200, { 'Content-Type': 'text/plain' });
106+
res.write('Hello, World!');
107+
res.end();
108+
```
109+
## IncomingMessage
110+
111+
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.
112+
113+
```js
114+
import { get, IncomingMessage } from 'node:http';
115+
import { ok, strictEqual } from 'node:assert';
116+
117+
get('http://docs.cloudflare.com', (res) => {
118+
strictEqual(res.statusCode, 301);
119+
ok(res instanceof IncomingMessage);
120+
});
121+
```
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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. 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:https';
20+
import { strictEqual } from 'node:assert';
21+
22+
const agent = new Agent();
23+
strictEqual(agent.protocol, 'https:');
24+
```
25+
26+
## get
27+
28+
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.
29+
30+
`get` methods behaves the same as `node:http` get method.
31+
32+
```js
33+
import { get } from 'node:https';
34+
import { strictEqual, ok } from 'node:assert';
35+
36+
get('https://developers.cloudflare.com/robots.txt', (res) => {
37+
strictEqual(res.statusCode, 200);
38+
let data = '';
39+
res.setEncoding('utf8');
40+
res.on('data', (chunk) => {
41+
data += chunk;
42+
});
43+
res.on('end', () => {
44+
// Returns the response body as a string.
45+
console.log(data);
46+
ok(data.includes('User-agent'));
47+
});
48+
});
49+
```
50+
51+
## request
52+
53+
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.
54+
55+
Request method accepts all options from `http.request` with some differences in default values:
56+
- `protocol`: default `https:`
57+
- `port`: default `443`
58+
- `agent`: default `https.globalAgent`
59+
60+
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.
61+
62+
```js
63+
import { request } from 'node:https';
64+
import { strictEqual, ok } from 'node:assert';
65+
66+
const req = request('https://developers.cloudflare.com/robots.txt', {
67+
method: 'GET',
68+
}, (res) => {
69+
strictEqual(res.statusCode, 200);
70+
let data = '';
71+
res.setEncoding('utf8');
72+
res.on('data', (chunk) => {
73+
data += chunk;
74+
});
75+
res.on('end', () => {
76+
// Returns the response body as a string.
77+
console.log(data);
78+
ok(data.includes('User-agent'));
79+
});
80+
});
81+
req.end();
82+
```

0 commit comments

Comments
 (0)