Skip to content

Commit ae95ec6

Browse files
committed
Further tweaks to nodejs docs and changelog
1 parent 55213cc commit ae95ec6

File tree

4 files changed

+47
-39
lines changed

4 files changed

+47
-39
lines changed

src/content/changelogs-next/2025-01-22-nodejs-compat-improvements.mdx

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,32 @@ When using a Worker with the [`nodejs_compat`](/workers/runtime-apis/nodejs/) co
1717

1818
#### node:net
1919

20-
You can use [`net.Socket`](https://nodejs.org/api/net.html#class-netsocket) to create a direct connection to a server via a TCP socket.
21-
This can be useful when connecting to databases such as [MySQL](https://www.mysql.com/) or [PostgreSQL](https://www.postgresql.org/),
22-
though if possible, using [Hyperdrive Connection Pooling](/hyperdrive/configuration/how-hyperdrive-works/#connection-pooling) is preferable.
20+
This makes it possible to connect to databases such as [MySQL](https://www.mysql.com/), [PostgreSQL](https://www.postgresql.org/),
21+
[Redis](https://redis.io/), or [MongoDB](https://github.com/mongodb/mongo). Though for production use, we recommend that you connect
22+
using TLS, which can be done with the [`cloudflare:sockets`](/workers/runtime-apis/tcp-sockets/#connect)
23+
module, or prefereably by using [Hyperdrive](/hyperdrive), which includes
24+
[connection pooling](/hyperdrive/configuration/how-hyperdrive-works/#connection-pooling)
25+
and [query caching](/hyperdrive/configuration/how-hyperdrive-works/#query-caching).
2326

2427
<TypeScriptExample filename="index.ts">
2528
```ts
26-
import net from 'node:net';
29+
import net from "node:net";
2730

28-
const exampleIP = '127.0.0.1';
31+
const exampleIP = "127.0.0.1";
2932

3033
export default {
31-
async fetch(req): Promise<Response> {
32-
const socket = new net.Socket();
33-
socket.connect(4000, exampleIP, function () {
34-
console.log('Connected');
35-
});
34+
async fetch(req): Promise<Response> {
35+
const socket = new net.Socket();
36+
socket.connect(4000, exampleIP, function () {
37+
console.log("Connected");
38+
});
3639

37-
socket.write('Hello, Server!');
38-
socket.end();
39-
40-
return new Response('Wrote to server', { status: 200 });
41-
},
40+
socket.write("Hello, Server!");
41+
socket.end();
4242

43+
return new Response("Wrote to server", { status: 200 });
44+
},
4345
} satisfies ExportedHandler;
44-
4546
````
4647
</TypeScriptExample>
4748

@@ -77,16 +78,15 @@ and [`setImmediate`](https://nodejs.org/api/timers.html#setimmediatecallback-arg
7778
7879
<TypeScriptExample filename="index.ts">
7980
```ts
80-
import timers from 'node:timers';
81+
import timers from "node:timers";
8182

8283
console.log("first");
8384
timers.setTimeout(() => {
84-
console.log("last");
85+
console.log("last");
8586
}, 10);
8687

8788
timers.setTimeout(() => {
88-
console.log("next");
89+
console.log("next");
8990
});
90-
9191
```
9292
</TypeScriptExample>

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ import { Render, TypeScriptExample } from "~/components";
1010
You can use [`node:dns`](https://nodejs.org/api/dns.html) for name resolution via DNS lookups using
1111
[Cloudflare DNS](https://www.cloudflare.com/application-services/products/dns/) at 1.1.1.1.
1212

13-
```js
13+
<TypeScriptExample filename="index.ts">
14+
```ts
1415
import dns from "node:dns";
1516

1617
dns.lookup("example.org", (_err: any, address: string, ipFamily: number) =>
1718
console.log(`address: ${address} family: IPv${ipFamily}`);
1819
});
1920
```
21+
</TypeScriptExample>
2022

2123
:::note
2224

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

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,36 @@ import { Render, TypeScriptExample } from "~/components";
88
<Render file="nodejs-compat-howto" />
99

1010
You can use [`node:net`](https://nodejs.org/api/net.html) to create a direct connection to servers via a TCP sockets
11-
with [`net.Socket`](https://nodejs.org/api/net.html#class-netsocket). This can be useful when connecting to databases such as MySQL or PostgreSQL, though if possible, using
12-
[Hyperdrive Connection Pooling](/hyperdrive/configuration/how-hyperdrive-works/#connection-pooling) is preferable.
11+
with [`net.Socket`](https://nodejs.org/api/net.html#class-netsocket).
12+
13+
This makes it possible to connect to databases such as [MySQL](https://www.mysql.com/), [PostgreSQL](https://www.postgresql.org/),
14+
[Redis](https://redis.io/), or [MongoDB](https://github.com/mongodb/mongo). Though for production use, we recommend that you connect
15+
using TLS, which can be done with the [`cloudflare:sockets`](/workers/runtime-apis/tcp-sockets/#connect)
16+
module, or prefereably by using [Hyperdrive](/hyperdrive), which includes
17+
[connection pooling](/hyperdrive/configuration/how-hyperdrive-works/#connection-pooling)
18+
and [query caching](/hyperdrive/configuration/how-hyperdrive-works/#query-caching).
1319

1420
These functions use [`connect`](/workers/runtime-apis/tcp-sockets/#connect) functionality from the built-in `cloudflare:sockets` module.
1521

1622
<TypeScriptExample filename="index.ts">
1723
```ts
18-
import net from 'node:net';
24+
import net from "node:net";
1925

20-
const exampleIP = '127.0.0.1';
26+
const exampleIP = "127.0.0.1";
2127

2228
export default {
23-
async fetch(req): Promise<Response> {
24-
const socket = new net.Socket();
25-
socket.connect(4000, exampleIP, function () {
26-
console.log('Connected');
27-
});
29+
async fetch(req): Promise<Response> {
30+
const socket = new net.Socket();
31+
socket.connect(4000, exampleIP, function () {
32+
console.log("Connected");
33+
});
2834

29-
socket.write('Hello, Server!');
30-
socket.end();
35+
socket.write("Hello, Server!");
36+
socket.end();
3137

32-
return new Response('Wrote to server', { status: 200 });
33-
},
38+
return new Response("Wrote to server", { status: 200 });
3439

40+
},
3541
} satisfies ExportedHandler;
3642

3743
```
@@ -43,3 +49,4 @@ and [`net.SocketAddress`](https://nodejs.org/api/net.html#class-netsocketaddress
4349
Note that the [`net.Server`](https://nodejs.org/api/net.html#class-netserver) class is not supported by Workers.
4450

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

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,16 @@ and [`setImmediate`](https://nodejs.org/api/timers.html#setimmediatecallback-arg
1515

1616
<TypeScriptExample filename="index.ts">
1717
```ts
18-
import timers from 'node:timers';
18+
import timers from "node:timers";
1919

20-
console.log('first');
20+
console.log("first");
2121
timers.setTimeout(() => {
22-
console.log('last');
22+
console.log("last");
2323
}, 10);
2424

2525
timers.setTimeout(() => {
26-
console.log('next');
26+
console.log("next");
2727
});
28-
2928
```
3029
</TypeScriptExample>
3130

0 commit comments

Comments
 (0)