跳转到主要内容

net

import { Render, TypeScriptExample } from "~/components";

You can use node:net to create a direct connection to servers via a TCP sockets with net.Socket.

These functions use connect functionality from the built-in cloudflare:sockets module.

```ts import net from "node:net";

const exampleIP = "127.0.0.1";

export default { async fetch(req): Promise { const socket = new net.Socket(); socket.connect(4000, exampleIP, function () { console.log("Connected"); });

socket.write("Hello, Server!");
socket.end();

return new Response("Wrote to server", { status: 200 });

}, } satisfies ExportedHandler;

</TypeScriptExample>

Additionally, other APIs such as [`net.BlockList`](https://nodejs.org/api/net.html#class-netblocklist)
and [`net.SocketAddress`](https://nodejs.org/api/net.html#class-netsocketaddress) are available.

Note that the [`net.Server`](https://nodejs.org/api/net.html#class-netserver) class is not supported by Workers.

The full `node:net` API is documented in the [Node.js documentation for `node:net`](https://nodejs.org/api/net.html).