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
Copy file name to clipboardExpand all lines: src/content/docs/workers/runtime-apis/nodejs/http.mdx
+95-56Lines changed: 95 additions & 56 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,67 +13,80 @@ An implementation of the Node.js [`http.get`](https://nodejs.org/docs/latest/api
13
13
14
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.
15
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
+
34
50
## request
35
51
36
52
An implementation of the Node.js [`http.request'](https://nodejs.org/docs/latest/api/http.html#httprequesturl-options-callback) method.
37
53
38
-
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.
39
59
40
60
```js
41
-
import { request } from'node:http';
42
-
import { strictEqual } from'node:assert';
61
+
import { get } from'node:http';
43
62
44
-
constreq=request({
45
-
method:'GET',
46
-
protocol:'http:',
47
-
hostname:'docs.cloudflare.com',
48
-
path:'/'
49
-
}, (res) => {
50
-
// requests to http://docs.cloudflare.com get redirected to their https counterpart.
// requests to http://docs.cloudflare.com get redirected to their https counterpart.
70
-
strictEqual(res.statusCode, 301);
71
-
});
72
-
73
-
req.end();
74
-
```
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:
75
89
76
-
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:
77
90
-`maxHeaderSize`
78
91
-`insecureHTTPParser`
79
92
-`createConnection`
@@ -82,16 +95,23 @@ The following options passed to the `request` method are not supported due to th
82
95
83
96
## OutgoingMessage
84
97
85
-
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.
@@ -100,10 +120,15 @@ The `IncomingMessage` class represents an HTTP request that is received from the
100
120
import { get, IncomingMessage } from'node:http';
101
121
import { ok, strictEqual } from'node:assert';
102
122
103
-
get('http://docs.cloudflare.com', (res) => {
104
-
strictEqual(res.statusCode, 301);
105
-
ok(res instanceof IncomingMessage);
106
-
});
123
+
exportdefault {
124
+
asyncfetch() {
125
+
// ...
126
+
get('http://example.org', (res) => {
127
+
ok(res instanceof IncomingMessage);
128
+
});
129
+
// ...
130
+
}
131
+
}
107
132
```
108
133
109
134
## Agent
@@ -133,3 +158,17 @@ export default {
133
158
}
134
159
}
135
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.
Copy file name to clipboardExpand all lines: src/content/docs/workers/runtime-apis/nodejs/https.mdx
+67-38Lines changed: 67 additions & 38 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,64 +11,78 @@ import { Render } from "~/components";
11
11
12
12
An implementation of the Node.js [`https.get'](https://nodejs.org/docs/latest/api/https.html#httpsgetoptions-callback) method.
13
13
14
-
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. This is a convenience method that simplifies making HTTPS GET requests without manually configuring request options.
14
+
The `get` method performs a GET request to the specified URL and invokes the callback with the response. This is a convenience method that simplifies making HTTPS GET requests without manually configuring request options.
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
+
38
50
## request
39
51
40
52
An implementation of the Node.js [`https.request'](https://nodejs.org/docs/latest/api/https.html#httpsrequestoptions-callback) method.
41
53
42
-
The [`request`](https://nodejs.org/docs/latest/api/https.html#httpsrequestoptions-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://developers.cloudflare.com/workers/runtime-apis/streams/writablestream/) for sending request data.
43
-
44
-
Request method accepts all options from `http.request` with some differences in default values:
54
+
The `request` method creates an HTTPS 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.
45
55
46
-
-`protocol`: default `https:`
47
-
-`port`: default `443`
48
-
-`agent`: default `https.globalAgent`
56
+
Because `get` 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 `get` will throw
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`.
@@ -100,3 +114,18 @@ export default {
100
114
}
101
115
}
102
116
```
117
+
118
+
## Other differences between Node.js and Workers implementation of `node:https`
119
+
120
+
Because the Workers imlementation of `node:https` is a wrapper around the global `fetch` API, there are some differences in behavior compared to Node.js:
121
+
122
+
*`Connection` headers are not used. Workers will manage connections automatically.
123
+
*`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.
124
+
*`Expect: 100-continue` headers are not supported.
125
+
* Trailing headers are not supported.
126
+
* The `'continue'` event is not supported.
127
+
* The `'information'` event is not supported.
128
+
* The `'socket'` event is not supported.
129
+
* The `'upgrade'` event is not supported.
130
+
* Gaining direct access to the underlying `socket` is not supported.
131
+
* Configuring TLS-specific options like `ca`, `cert`, `key`, `rejectUnauthorized`, etc, is not supported.
0 commit comments