Skip to content

Commit 68d2271

Browse files
committed
add node:http and node:https documentation
1 parent b13ce04 commit 68d2271

File tree

3 files changed

+258
-26
lines changed

3 files changed

+258
-26
lines changed

src/content/docs/workers/runtime-apis/nodejs/http.mdx

Lines changed: 164 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,39 @@ import { Render } from "~/components"
77

88
<Render file="nodejs-compat-howto" />
99

10+
## Compatibility flags
11+
12+
### Client-side methods
13+
14+
To use the HTTP client-side methods (`http.get`, `http.request`, etc.), you must enable the [`enable_nodejs_http_modules`](/workers/runtime-apis/compatibility-dates/#enable-nodejs-http-client-modules) compatibility flag in addition to the [`nodejs_compat`](/workers/runtime-apis/nodejs/) flag.
15+
16+
This flag is automatically enabled for Workers using a [compatibility date](/workers/runtime-apis/compatibility-dates/) of `2025-08-15` or later when `nodejs_compat` is enabled. For Workers using an earlier compatibility date, you can manually enable it by adding the flag to your `wrangler.toml`:
17+
18+
```toml
19+
compatibility_flags = ["nodejs_compat", "enable_nodejs_http_modules"]
20+
```
21+
22+
### Server-side methods
23+
24+
To use the HTTP server-side methods (`http.createServer`, `http.Server`, `http.ServerResponse`), you must enable the `enable_nodejs_http_server_modules` compatibility flag in addition to the [`nodejs_compat`](/workers/runtime-apis/nodejs/) flag.
25+
26+
This flag is automatically enabled for Workers using a [compatibility date](/workers/runtime-apis/compatibility-dates/) of `2025-09-01` or later when `nodejs_compat` is enabled. For Workers using an earlier compatibility date, you can manually enable it by adding the flag to your `wrangler.toml`:
27+
28+
```toml
29+
compatibility_flags = ["nodejs_compat", "enable_nodejs_http_server_modules"]
30+
```
31+
32+
To use both client-side and server-side methods, enable both flags:
33+
34+
```toml
35+
compatibility_flags = ["nodejs_compat", "enable_nodejs_http_modules", "enable_nodejs_http_server_modules"]
36+
```
37+
1038
## Agent
1139

12-
An implementation of the Node.js [`http.Agent'](https://nodejs.org/docs/latest/api/http.html#class-httpagent) class.
40+
An implementation of the Node.js [`http.Agent`](https://nodejs.org/docs/latest/api/http.html#class-httpagent) class.
1341

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.
42+
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 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.
1943

2044
```js
2145
import { Agent } from 'node:http';
@@ -51,13 +75,13 @@ get('http://docs.cloudflare.com/robots.txt', (res) => {
5175

5276
## request
5377

54-
An implementation of the Node.js [`http.request'](https://nodejs.org/docs/latest/api/http.html#httprequesturl-options-callback) method.
78+
An implementation of the Node.js [`http.request`](https://nodejs.org/docs/latest/api/http.html#httprequesturl-options-callback) method.
5579

5680
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.
5781

5882
```js
5983
import { request } from 'node:http';
60-
import { strictEqual } from 'node:assert';
84+
import { strictEqual, ok } from 'node:assert';
6185

6286
const req = request({
6387
method: 'GET',
@@ -81,7 +105,10 @@ req.end();
81105
```
82106

83107
```js
84-
const req = request(new URL('http://docs.cloudflare.com'),{
108+
import { request } from 'node:http';
109+
import { strictEqual } from 'node:assert';
110+
111+
const req = request(new URL('http://docs.cloudflare.com'), {
85112
method: 'GET',
86113
}, (res) => {
87114
// requests to http://docs.cloudflare.com get redirected to their https counterpart.
@@ -91,7 +118,7 @@ const req = request(new URL('http://docs.cloudflare.com'),{
91118
req.end();
92119
```
93120

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:
121+
The following options passed to the `request` method are not supported due to differences in the Cloudflare Workers implementation of the `node:http` module:
95122
- `maxHeaderSize`
96123
- `insecureHTTPParser`
97124
- `createConnection`
@@ -100,7 +127,9 @@ The following options passed to the `request` method are not supported due to th
100127

101128
## OutgoingMessage
102129

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).
130+
An implementation of the Node.js [`http.OutgoingMessage`](https://nodejs.org/docs/latest/api/http.html#class-httpoutgoingmessage) class.
131+
132+
The `OutgoingMessage` class is a base class for outgoing HTTP messages (both requests and responses). It provides methods for writing headers and body data, as well as for ending the message. `OutgoingMessage` extends from the [`Writable` stream class](https://nodejs.org/docs/latest/api/stream.html#class-streamwritable).
104133

105134
```js
106135
import { OutgoingMessage } from 'node:http';
@@ -110,9 +139,12 @@ res.writeHead(200, { 'Content-Type': 'text/plain' });
110139
res.write('Hello, World!');
111140
res.end();
112141
```
142+
113143
## IncomingMessage
114144

115-
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.
145+
An implementation of the Node.js [`http.IncomingMessage`](https://nodejs.org/docs/latest/api/http.html#class-httpincomingmessage) class.
146+
147+
The `IncomingMessage` class represents an HTTP message (request or response). It provides methods for reading headers and body data. `IncomingMessage` extends from the `Readable` stream class.
116148

117149
```js
118150
import { get, IncomingMessage } from 'node:http';
@@ -123,3 +155,123 @@ get('http://docs.cloudflare.com', (res) => {
123155
ok(res instanceof IncomingMessage);
124156
});
125157
```
158+
159+
The Workers implementation includes a `cloudflare` property on `IncomingMessage` objects:
160+
161+
```js
162+
import { get } from 'node:http';
163+
164+
get('http://example.com', (res) => {
165+
// Access Cloudflare-specific request properties
166+
console.log(res.cloudflare.cf.country);
167+
console.log(res.cloudflare.cf.ray);
168+
});
169+
```
170+
171+
The `cloudflare.cf` property contains [Cloudflare-specific request properties](/workers/runtime-apis/request/#incomingrequestcfproperties).
172+
173+
The following differences exist between the Workers implementation and Node.js:
174+
175+
- Trailer headers are not supported
176+
- The `socket` attribute only contains the following properties: `encrypted`, `remoteFamily`, `remoteAddress`, `remotePort`, `localAddress`, `localPort`, and `destroy()` method
177+
- The `socket` attribute does not extend from `net.Socket`
178+
179+
## createServer
180+
181+
An implementation of the Node.js [`http.createServer`](https://nodejs.org/docs/latest/api/http.html#httpcreateserveroptions-requestlistener) method.
182+
183+
The `createServer` method creates an HTTP server instance that can handle incoming requests. It's a convenience function that creates a new `Server` instance and optionally sets up a request listener callback.
184+
185+
```js
186+
import { createServer } from 'node:http';
187+
import { httpServerHandler } from 'cloudflare:node';
188+
189+
const server = createServer((req, res) => {
190+
res.writeHead(200, { 'Content-Type': 'text/plain' });
191+
res.end('Hello from Node.js HTTP server!');
192+
});
193+
194+
server.listen(8080);
195+
export default httpServerHandler({ port: 8080 });
196+
```
197+
198+
The `httpServerHandler` function integrates Node.js HTTP servers with the Cloudflare Workers request model. When a request arrives at your Worker, the handler automatically routes it to your Node.js server running on the specified port. This bridge allows you to use familiar Node.js server patterns while benefiting from the Workers runtime environment, including automatic scaling, edge deployment, and integration with other Cloudflare services.
199+
200+
:::note
201+
Failing to call `close()` on an HTTP server may result in the server being leaked. To prevent this, call `close()` when you're done with the server, or use explicit resource management:
202+
203+
```js
204+
import { createServer } from 'node:http';
205+
206+
await using server = createServer((req, res) => {
207+
res.end('Hello World');
208+
});
209+
// Server will be automatically closed when it goes out of scope
210+
```
211+
:::
212+
213+
## Server
214+
215+
An implementation of the Node.js [`http.Server`](https://nodejs.org/docs/latest/api/http.html#class-httpserver) class.
216+
217+
The `Server` class represents an HTTP server and provides methods for handling incoming requests. It extends the Node.js `EventEmitter` class and can be used to create custom server implementations.
218+
219+
```js
220+
import { Server } from 'node:http';
221+
222+
const server = new Server((req, res) => {
223+
res.writeHead(200, { 'Content-Type': 'application/json' });
224+
res.end(JSON.stringify({ message: 'Hello from HTTP Server!' }));
225+
});
226+
```
227+
228+
The following differences exist between the Workers implementation and Node.js:
229+
230+
- Connection management methods such as `closeAllConnections()` and `closeIdleConnections()` are not implemented
231+
- Only `listen()` variants with a port number or no parameters are supported: `listen()`, `listen(0, callback)`, `listen(callback)`, etc.
232+
- The following server options are not supported: `maxHeaderSize`, `insecureHTTPParser`, `keepAliveTimeout`, `connectionsCheckingInterval`
233+
234+
## ServerResponse
235+
236+
An implementation of the Node.js [`http.ServerResponse`](https://nodejs.org/docs/latest/api/http.html#class-httpserverresponse) class.
237+
238+
The `ServerResponse` class represents the server-side response object that is passed to request handlers. It provides methods for writing response headers and body data, and extends the Node.js `Writable` stream class.
239+
240+
```js
241+
import { createServer, ServerResponse } from 'node:http';
242+
import { ok } from 'node:assert';
243+
244+
const server = createServer((req, res) => {
245+
ok(res instanceof ServerResponse);
246+
res.statusCode = 200;
247+
res.setHeader('Content-Type', 'text/html');
248+
res.write('<h1>Hello World</h1>');
249+
res.end();
250+
});
251+
```
252+
253+
```js
254+
import { createServer } from 'node:http';
255+
256+
const server = createServer((req, res) => {
257+
// Set multiple headers at once
258+
res.writeHead(200, {
259+
'Content-Type': 'application/json',
260+
'X-Custom-Header': 'Workers-HTTP'
261+
});
262+
263+
// Stream response data
264+
res.write('{"data": [');
265+
res.write('{"id": 1, "name": "Item 1"},');
266+
res.write('{"id": 2, "name": "Item 2"}');
267+
res.write(']}');
268+
269+
// End the response
270+
res.end();
271+
});
272+
```
273+
274+
The following methods and features are not supported in the Workers implementation:
275+
276+
- `assignSocket()` and `detachSocket()` methods are not available
277+
- Trailer headers are not supported

src/content/docs/workers/runtime-apis/nodejs/https.mdx

Lines changed: 92 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,39 @@ import { Render } from "~/components";
77

88
<Render file="nodejs-compat-howto" />
99

10+
## Compatibility flags
11+
12+
### Client-side methods
13+
14+
To use the HTTPS client-side methods (`https.get`, `https.request`, etc.), you must enable the [`enable_nodejs_http_modules`](/workers/runtime-apis/compatibility-dates/#enable-nodejs-http-client-modules) compatibility flag in addition to the [`nodejs_compat`](/workers/runtime-apis/nodejs/) flag.
15+
16+
This flag is automatically enabled for Workers using a [compatibility date](/workers/runtime-apis/compatibility-dates/) of `2025-08-15` or later when `nodejs_compat` is enabled. For Workers using an earlier compatibility date, you can manually enable it by adding the flag to your `wrangler.toml`:
17+
18+
```toml
19+
compatibility_flags = ["nodejs_compat", "enable_nodejs_http_modules"]
20+
```
21+
22+
### Server-side methods
23+
24+
To use the HTTPS server-side methods (`https.createServer`, `https.Server`, `https.ServerResponse`), you must enable the `enable_nodejs_http_server_modules` compatibility flag in addition to the [`nodejs_compat`](/workers/runtime-apis/nodejs/) flag.
25+
26+
This flag is automatically enabled for Workers using a [compatibility date](/workers/runtime-apis/compatibility-dates/) of `2025-09-01` or later when `nodejs_compat` is enabled. For Workers using an earlier compatibility date, you can manually enable it by adding the flag to your `wrangler.toml`:
27+
28+
```toml
29+
compatibility_flags = ["nodejs_compat", "enable_nodejs_http_server_modules"]
30+
```
31+
32+
To use both client-side and server-side methods, enable both flags:
33+
34+
```toml
35+
compatibility_flags = ["nodejs_compat", "enable_nodejs_http_modules", "enable_nodejs_http_server_modules"]
36+
```
37+
1038
## Agent
1139

12-
An implementation of the Node.js [`https.Agent'](https://nodejs.org/docs/latest/api/https.html#class-httpsagent) class.
40+
An implementation of the Node.js [`https.Agent`](https://nodejs.org/docs/latest/api/https.html#class-httpsagent) class.
1341

14-
An [Agent](https://nodejs.org/docs/latest/api/https.html#class-httpsagent) manages HTTPS 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.
42+
An [Agent](https://nodejs.org/docs/latest/api/https.html#class-httpsagent) manages HTTPS 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.
1943

2044
```js
2145
import { Agent } from "node:https";
@@ -27,17 +51,17 @@ strictEqual(agent.protocol, "https:");
2751

2852
## get
2953

30-
An implementation of the Node.js [`https.get'](https://nodejs.org/docs/latest/api/https.html#httpsgetoptions-callback) method.
54+
An implementation of the Node.js [`https.get`](https://nodejs.org/docs/latest/api/https.html#httpsgetoptions-callback) method.
3155

32-
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.
56+
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.
3357

3458
```js
3559
import { get } from "node:https";
3660

3761
export default {
3862
async fetch() {
3963
const { promise, resolve, reject } = Promise.withResolvers();
40-
get("http://example.com", (res) => {
64+
get("https://example.com", (res) => {
4165
let data = "";
4266
res.setEncoding("utf8");
4367
res.on("data", (chunk) => {
@@ -55,11 +79,11 @@ export default {
5579

5680
## request
5781

58-
An implementation of the Node.js [`https.request'](https://nodejs.org/docs/latest/api/https.html#httpsrequestoptions-callback) method.
82+
An implementation of the Node.js [`https.request`](https://nodejs.org/docs/latest/api/https.html#httpsrequestoptions-callback) method.
5983

60-
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.
84+
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 [writable stream](https://nodejs.org/docs/latest/api/stream.html#class-streamwritable) for sending request data.
6185

62-
Request method accepts all options from `http.request` with some differences in default values:
86+
The request method accepts all options from `http.request` with some differences in default values:
6387

6488
- `protocol`: default `https:`
6589
- `port`: default `443`
@@ -90,3 +114,59 @@ req.end();
90114
```
91115

92116
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`.
117+
118+
## createServer
119+
120+
An implementation of the Node.js [`https.createServer`](https://nodejs.org/docs/latest/api/https.html#httpscreateserveroptions-requestlistener) method.
121+
122+
The `createServer` method creates an HTTPS server instance that can handle incoming secure requests. It's a convenience function that creates a new `Server` instance and optionally sets up a request listener callback.
123+
124+
```js
125+
import { createServer } from 'node:https';
126+
import { httpServerHandler } from 'cloudflare:node';
127+
128+
const server = createServer((req, res) => {
129+
res.writeHead(200, { 'Content-Type': 'text/plain' });
130+
res.end('Hello from Node.js HTTPS server!');
131+
});
132+
133+
server.listen(8080);
134+
export default httpServerHandler({ port: 8080 });
135+
```
136+
137+
The `httpServerHandler` function integrates Node.js HTTPS servers with the Cloudflare Workers request model. When a request arrives at your Worker, the handler automatically routes it to your Node.js server running on the specified port. This bridge allows you to use familiar Node.js server patterns while benefiting from the Workers runtime environment, including automatic scaling, edge deployment, and integration with other Cloudflare services.
138+
139+
:::note
140+
Failing to call `close()` on an HTTPS server may result in the server being leaked. To prevent this, call `close()` when you're done with the server, or use explicit resource management:
141+
142+
```js
143+
import { createServer } from 'node:https';
144+
145+
await using server = createServer((req, res) => {
146+
res.end('Hello World');
147+
});
148+
// Server will be automatically closed when it goes out of scope
149+
```
150+
:::
151+
152+
## Server
153+
154+
An implementation of the Node.js [`https.Server`](https://nodejs.org/docs/latest/api/https.html#class-httpsserver) class.
155+
156+
The `Server` class represents an HTTPS server and provides methods for handling incoming secure requests. It extends the Node.js `EventEmitter` class and can be used to create custom secure server implementations.
157+
158+
```js
159+
import { Server } from 'node:https';
160+
161+
const server = new Server((req, res) => {
162+
res.writeHead(200, { 'Content-Type': 'application/json' });
163+
res.end(JSON.stringify({ message: 'Hello from HTTPS Server!' }));
164+
});
165+
```
166+
167+
The following differences exist between the Workers implementation and Node.js:
168+
169+
- Connection management methods such as `closeAllConnections()` and `closeIdleConnections()` are not implemented
170+
- Only `listen()` variants with a port number or no parameters are supported: `listen()`, `listen(0, callback)`, `listen(callback)`, etc.
171+
- The following server options are not supported: `maxHeaderSize`, `insecureHTTPParser`, `keepAliveTimeout`, `connectionsCheckingInterval`
172+
- TLS/SSL-specific options such as `ca`, `cert`, `key`, `pfx`, `rejectUnauthorized`, `secureProtocol` are not supported in the Workers environment

0 commit comments

Comments
 (0)