Skip to content

Commit c17c13c

Browse files
committed
add cloudflare:node method docs
1 parent aa9d87d commit c17c13c

File tree

1 file changed

+47
-1
lines changed
  • src/content/docs/workers/runtime-apis/nodejs

1 file changed

+47
-1
lines changed

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

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,53 @@ server.listen(8080);
228228
export default httpServerHandler({ port: 8080 });
229229
```
230230

231-
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.
231+
## Node.js integration
232+
233+
### httpServerHandler
234+
235+
The `httpServerHandler` function integrates Node.js HTTP servers with the Cloudflare Workers request model. It supports two API patterns:
236+
237+
```js
238+
import http from "node:http";
239+
import { httpServerHandler } from "cloudflare:node";
240+
241+
const server = http.createServer((req, res) => {
242+
res.end("hello world");
243+
});
244+
245+
// Pass server directly (simplified) - automatically calls listen() if needed
246+
export default httpServerHandler(server);
247+
248+
// Or use port-based routing for multiple servers
249+
server.listen(8080);
250+
export default httpServerHandler({ port: 8080 });
251+
```
252+
253+
The handler automatically routes incoming Worker requests to your Node.js server. When using port-based routing, the port number acts as a routing key to determine which server handles requests, allowing multiple servers to coexist in the same Worker.
254+
255+
### handleAsNodeRequest
256+
257+
For more direct control over request routing, you can use the `handleAsNodeRequest` function from `cloudflare:node`. This function directly routes a Worker request to a Node.js server running on a specific port:
258+
259+
```js
260+
import { createServer } from "node:http";
261+
import { handleAsNodeRequest } from "cloudflare:node";
262+
263+
const server = createServer((req, res) => {
264+
res.writeHead(200, { "Content-Type": "text/plain" });
265+
res.end("Hello from Node.js HTTP server!");
266+
});
267+
268+
server.listen(8080);
269+
270+
export default {
271+
fetch(request) {
272+
return handleAsNodeRequest(8080, request);
273+
},
274+
};
275+
```
276+
277+
This approach gives you full control over the fetch handler while still leveraging Node.js HTTP servers for request processing.
232278

233279
:::note
234280
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).

0 commit comments

Comments
 (0)