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
@@ -42,11 +42,11 @@ An implementation of the Node.js [`http.Agent`](https://nodejs.org/docs/latest/a
42
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.
43
43
44
44
```js
45
-
import { Agent } from'node:http';
46
-
import { strictEqual } from'node:assert';
45
+
import { Agent } from"node:http";
46
+
import { strictEqual } from"node:assert";
47
47
48
48
constagent=newAgent();
49
-
strictEqual(agent.protocol, 'http:');
49
+
strictEqual(agent.protocol, "http:");
50
50
```
51
51
52
52
## get
@@ -56,21 +56,29 @@ An implementation of the Node.js [`http.get`](https://nodejs.org/docs/latest/api
56
56
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.
// requests to http://docs.cloudflare.com get redirected to their https counterpart.
67
+
strictEqual(res.statusCode, 301);
68
+
let data ="";
69
+
res.setEncoding("utf8");
70
+
res.on("data", (chunk) => {
71
+
data += chunk;
72
+
});
73
+
res.once("error", reject);
74
+
res.on("end", () => {
75
+
ok(data.includes("301 Moved Permanently"));
76
+
resolve(newResponse(data));
77
+
});
78
+
});
79
+
return promise;
80
+
},
81
+
};
74
82
```
75
83
76
84
## request
@@ -80,45 +88,43 @@ An implementation of the Node.js [`http.request`](https://nodejs.org/docs/latest
80
88
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.
81
89
82
90
```js
83
-
import { request } from'node:http';
84
-
import { strictEqual, ok } from'node:assert';
85
-
86
-
constreq=request({
87
-
method:'GET',
88
-
protocol:'http:',
89
-
hostname:'docs.cloudflare.com',
90
-
path:'/'
91
-
}, (res) => {
92
-
// requests to http://docs.cloudflare.com get redirected to their https counterpart.
// requests to http://docs.cloudflare.com get redirected to their https counterpart.
106
+
strictEqual(res.statusCode, 301);
107
+
108
+
let data ="";
109
+
res.setEncoding("utf8");
110
+
res.on("data", (chunk) => {
111
+
data += chunk;
112
+
});
113
+
res.once("error", reject);
114
+
res.on("end", () => {
115
+
ok(data.includes("301 Moved Permanently"));
116
+
resolve(newResponse(data));
117
+
});
118
+
},
119
+
);
120
+
req.end();
121
+
return promise;
122
+
},
123
+
};
119
124
```
120
125
121
126
The following options passed to the `request` method are not supported due to differences in the Cloudflare Workers implementation of the `node:http` module:
127
+
122
128
-`maxHeaderSize`
123
129
-`insecureHTTPParser`
124
130
-`createConnection`
@@ -131,14 +137,7 @@ An implementation of the Node.js [`http.OutgoingMessage`](https://nodejs.org/doc
131
137
132
138
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).
Both `ClientRequest` and `ServerResponse` both extend from and inherit from `OutgoingMessage`.
142
141
143
142
## IncomingMessage
144
143
@@ -147,66 +146,76 @@ An implementation of the Node.js [`http.IncomingMessage`](https://nodejs.org/doc
147
146
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.
The `cloudflare.cf` property contains [Cloudflare-specific request properties](/workers/runtime-apis/request/#incomingrequestcfproperties).
172
184
173
185
The following differences exist between the Workers implementation and Node.js:
174
186
175
187
- Trailer headers are not supported
176
-
- The `socket` attribute **does not extend from `net.Socket`** and only contains the following properties: `encrypted`, `remoteFamily`, `remoteAddress`, `remotePort`, `localAddress`, `localPort`, and `destroy()` method
188
+
- The `socket` attribute **does not extend from `net.Socket`** and only contains the following properties: `encrypted`, `remoteFamily`, `remoteAddress`, `remotePort`, `localAddress`, `localPort`, and `destroy()` method.
189
+
- The following `socket` attributes behave differently than their Node.js counterparts:
190
+
-`remoteAddress` will return `127.0.0.1` when ran locally
191
+
-`remotePort` will return a random port number between 2^15 and 2^16
192
+
-`localAddress` will return the value of request's `host` header if exists. Otherwise, it will return `127.0.0.1`
193
+
-`localPort` will return the port number assigned to the server instance
194
+
-`req.socket.destroy()` falls through to `req.destroy()`
177
195
178
196
## createServer
179
197
180
198
An implementation of the Node.js [`http.createServer`](https://nodejs.org/docs/latest/api/http.html#httpcreateserveroptions-requestlistener) method.
181
199
182
-
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.
200
+
The `createServer` method creates an HTTP server instance that can handle incoming requests.
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.
215
+
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. Handler is using the specified port to determine which app to route the request to. 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.
198
216
199
217
:::note
200
-
Failing to call `close()` on an HTTP server may result in the server persisting until the worker is destroyed. In most cases, this is not an issue since servers typically live for the lifetime of the worker. However, if you need to create multiple servers during a worker's lifetime or want explicit lifecycle control (such as in test scenarios), call `close()` when you're done with the server, or use explicit resource management:
201
-
202
-
```js
203
-
import { createServer } from'node:http';
204
-
205
-
await using server =createServer((req, res) => {
206
-
res.end('Hello World');
207
-
});
208
-
// Server will be automatically closed when it goes out of scope
209
-
```
218
+
Failing to call `close()` on an HTTP server may result in the server persisting until the worker is destroyed. In most cases, this is not an issue since servers typically live for the lifetime of the worker. However, if you need to create multiple servers during a worker's lifetime or want explicit lifecycle control (such as in test scenarios), call `close()` when you're done with the server, or use [explicit resource management](https://v8.dev/features/explicit-resource-management).
210
219
:::
211
220
212
221
## Server
@@ -215,15 +224,15 @@ An implementation of the Node.js [`http.Server`](https://nodejs.org/docs/latest/
215
224
216
225
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.
217
226
218
-
When using `httpServerHandler`, the port number specified in `server.listen()` acts as a routing key rather than an actual network port. The handler uses this port to determine which HTTP server instance should handle incoming requests, allowing multiple servers to coexist within the same Worker by using different port numbers for identification.
227
+
When using `httpServerHandler`, the port number specified in `server.listen()` acts as a routing key rather than an actual network port. The handler uses this port to determine which HTTP server instance should handle incoming requests, allowing multiple servers to coexist within the same Worker by using different port numbers for identification. Using a port value of `0` (or `null` or `undefined`) will result in a random port number being assigned.
The following differences exist between the Workers implementation and Node.js:
234
243
235
244
- Connection management methods such as `closeAllConnections()` and `closeIdleConnections()` are not implemented
236
-
- Only `listen()` variants with a port number or no parameters are supported: `listen()`, `listen(0, callback)`, `listen(callback)`, etc.
245
+
- Only `listen()` variants with a port number or no parameters are supported: `listen()`, `listen(0, callback)`, `listen(callback)`, etc. For reference, see the [Node.js documentation](https://nodejs.org/docs/latest/api/net.html#serverlisten).
237
246
- The following server options are not supported: `maxHeaderSize`, `insecureHTTPParser`, `keepAliveTimeout`, `connectionsCheckingInterval`
238
247
239
248
## ServerResponse
@@ -243,23 +252,23 @@ An implementation of the Node.js [`http.ServerResponse`](https://nodejs.org/docs
243
252
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.
0 commit comments