You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Improve Agent example, move to end
Move the `Agent` part of the docs to the end since it's only
a stub impl and really isn't all that useful. Also improve the
example to show how one might actually use it.
* Improve and expand node:http docs, examples
@@ -7,91 +7,86 @@ import { Render } from "~/components"
7
7
8
8
<Renderfile="nodejs-compat-howto" />
9
9
10
-
## Agent
11
-
12
-
An implementation of the Node.js [`http.Agent'](https://nodejs.org/docs/latest/api/http.html#class-httpagent) class.
13
-
14
-
An [Agent](https://nodejs.org/docs/latest/api/http.html#class-httpagent) manages HTTP connection reuse by maintaining request queues per host/port. In the
15
-
workers environment, however, such low-level management of the network connection, ports,
16
-
etc, is not relevant because it is handled by the Cloudflare infrastructure instead. Accordingly, the
17
-
implementation of `Agent` in Workers is a stub implementation that does not support connection
18
-
pooling or keep-alive.
19
-
20
-
```js
21
-
import { Agent } from'node:http';
22
-
import { strictEqual } from'node:assert';
23
-
24
-
constagent=newAgent();
25
-
strictEqual(agent.protocol, 'http:');
26
-
```
27
-
28
10
## get
29
11
30
12
An implementation of the Node.js [`http.get`](https://nodejs.org/docs/latest/api/http.html#httpgetoptions-callback) method.
31
13
32
14
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.
33
15
16
+
Because `get` is a wrapper around `fetch(...)`, it may be used only within an exported
17
+
fetch or similar handler. Outside of such a handler, attempts to use `get` will throw
and is therefore subject to the same [limits](https://developers.cloudflare.com/workers/platform/limits/).
45
+
46
+
As shown in the example above, it is necessary to arrange for requests to be correctly
47
+
awaited in the `fetch` handler using a promise or the fetch may be canceled prematurely
48
+
when the handler returns.
49
+
52
50
## request
53
51
54
52
An implementation of the Node.js [`http.request'](https://nodejs.org/docs/latest/api/http.html#httprequesturl-options-callback) method.
55
53
56
-
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.
54
+
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 Node.js [stream.Writable](https://developers.cloudflare.com/workers/runtime-apis/nodejs/streams/) for sending request data.
55
+
56
+
Because `request` is a wrapper around `fetch(...)`, it may be used only within an exported
57
+
fetch or similar handler. Outside of such a handler, attempts to use `request` will throw
58
+
an error.
57
59
58
60
```js
59
-
import { request } from'node:http';
60
-
import { strictEqual } from'node:assert';
61
+
import { get } from'node:http';
61
62
62
-
constreq=request({
63
-
method:'GET',
64
-
protocol:'http:',
65
-
hostname:'docs.cloudflare.com',
66
-
path:'/'
67
-
}, (res) => {
68
-
// requests to http://docs.cloudflare.com get redirected to their https counterpart.
// requests to http://docs.cloudflare.com get redirected to their https counterpart.
88
-
strictEqual(res.statusCode, 301);
89
-
});
90
-
91
-
req.end();
92
-
```
88
+
The following options passed to the `request` (and `get`) method are not supported due to the differences required by Coudflare Workers implementation of `node:http` as a wrapper around the global `fetch` API:
93
89
94
-
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:
95
90
-`maxHeaderSize`
96
91
-`insecureHTTPParser`
97
92
-`createConnection`
@@ -100,16 +95,23 @@ The following options passed to the `request` method are not supported due to th
100
95
101
96
## OutgoingMessage
102
97
103
-
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
+
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 Node.js [`stream.Writable` stream class](https://developers.cloudflare.com/workers/runtime-apis/nodejs/streams/).
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.
@@ -118,8 +120,55 @@ The `IncomingMessage` class represents an HTTP request that is received from the
118
120
import { get, IncomingMessage } from'node:http';
119
121
import { ok, strictEqual } from'node:assert';
120
122
121
-
get('http://docs.cloudflare.com', (res) => {
122
-
strictEqual(res.statusCode, 301);
123
-
ok(res instanceof IncomingMessage);
124
-
});
123
+
exportdefault {
124
+
asyncfetch() {
125
+
// ...
126
+
get('http://example.org', (res) => {
127
+
ok(res instanceof IncomingMessage);
128
+
});
129
+
// ...
130
+
}
131
+
}
125
132
```
133
+
134
+
## Agent
135
+
136
+
A partial implementation of the Node.js [`http.Agent'](https://nodejs.org/docs/latest/api/http.html#class-httpagent) class.
137
+
138
+
An `Agent` manages HTTP connection reuse by maintaining request queues per host/port. In the workers environment, however, such low-level management of the network connection, ports, etc, is not relevant because it is handled by the Cloudflare infrastructure instead. Accordingly, the implementation of `Agent` in Workers is a stub implementation that does not support connection pooling or keep-alive.
139
+
140
+
```js
141
+
import { get, Agent } from'node:http';
142
+
import { strictEqual } from'node:assert';
143
+
144
+
exportdefault {
145
+
asyncfetch() {
146
+
147
+
constagent=newAgent();
148
+
get({
149
+
hostname:'example.org',
150
+
port:80,
151
+
path:'/',
152
+
agent, // possible but not all that useful.
153
+
}, (res) => {
154
+
// ...
155
+
});
156
+
157
+
returnnewResponse('ok');
158
+
}
159
+
}
160
+
```
161
+
162
+
## Other differences between Node.js and Workers implementation of `node:http`
163
+
164
+
Because the Workers implementation of `node:http` is a wrapper around the global `fetch` API, there are some differences in behavior and limitations compared to a standard Node.js environment:
165
+
166
+
*`Connection` headers are not used. Workers will manage connections automatically.
167
+
*`Content-Length` headers will be handled the same way as in the `fetch` API. If a body is provided, the header will be set automatically and manually set values will be ignored.
168
+
*`Expect: 100-continue` headers are not supported.
169
+
* Trailing headers are not supported.
170
+
* The `'continue'` event is not supported.
171
+
* The `'information'` event is not supported.
172
+
* The `'socket'` event is not supported.
173
+
* The `'upgrade'` event is not supported.
174
+
* Gaining direct access to the underlying `socket` is not supported.
0 commit comments