Skip to content

Commit 7c8660e

Browse files
committed
add node:http and node:https modules
1 parent 5a83835 commit 7c8660e

File tree

2 files changed

+199
-0
lines changed

2 files changed

+199
-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/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.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/strict';
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/strict';
116+
117+
get('http://docs.cloudflare.com', (res) => {
118+
strictEqual(res.statusCode, 301);
119+
ok(res instanceof IncomingMessage);
120+
});
121+
```
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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.setEncoding('utf8');
36+
res.on('data', (chunk) => {
37+
data += chunk;
38+
});
39+
res.on('end', () => {
40+
// Returns the response body as a string.
41+
console.log(data);
42+
ok(data.includes('User-agent'));
43+
});
44+
});
45+
```
46+
47+
## request
48+
49+
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.
50+
51+
Request method accepts all options from `http.request` with some differences in default values:
52+
- `protocol`: default `https:`
53+
- `port`: default `443`
54+
- `agent`: default `https.globalAgent`
55+
56+
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.
57+
58+
```js
59+
import { request } from 'node:https';
60+
import { strictEqual, ok } from 'node:assert/strict';
61+
62+
const req = request('https://developers.cloudflare.com/robots.txt', {
63+
method: 'GET',
64+
}, (res) => {
65+
strictEqual(res.statusCode, 200);
66+
let data = '';
67+
res.setEncoding('utf8');
68+
res.on('data', (chunk) => {
69+
data += chunk;
70+
});
71+
res.on('end', () => {
72+
// Returns the response body as a string.
73+
console.log(data);
74+
ok(data.includes('User-agent'));
75+
});
76+
});
77+
req.end();
78+
```

0 commit comments

Comments
 (0)