Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: add esm examples to node:net #55134

Merged
merged 2 commits into from
Sep 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 60 additions & 5 deletions doc/api/net.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ TCP or [IPC][] servers ([`net.createServer()`][]) and clients

It can be accessed using:

```js
```mjs
import net from 'node:net';
```

```cjs
const net = require('node:net');
```

Expand Down Expand Up @@ -1531,7 +1535,23 @@ Additional options:
Following is an example of a client of the echo server described
in the [`net.createServer()`][] section:

```js
```mjs
import net from 'node:net';
const client = net.createConnection({ port: 8124 }, () => {
// 'connect' listener.
console.log('connected to server!');
client.write('world!\r\n');
});
client.on('data', (data) => {
console.log(data.toString());
client.end();
});
client.on('end', () => {
console.log('disconnected from server');
});
```

```cjs
const net = require('node:net');
const client = net.createConnection({ port: 8124 }, () => {
// 'connect' listener.
Expand All @@ -1558,10 +1578,26 @@ option. In this case, the `onread` option will be only used to call
`new net.Socket([options])` and the `port` option will be used to
call `socket.connect(options[, connectListener])`.

```js
```mjs
import net from 'node:net';
import { Buffer } from 'node:buffer';
net.createConnection({
port: 8124,
onread: {
// Reuses a 4KiB Buffer for every read from the socket.
buffer: Buffer.alloc(4 * 1024),
callback: function(nread, buf) {
// Received data is available in `buf` from 0 to `nread`.
console.log(buf.toString('utf8', 0, nread));
},
},
});
```

```cjs
const net = require('node:net');
net.createConnection({
port: 80,
port: 8124,
onread: {
// Reuses a 4KiB Buffer for every read from the socket.
buffer: Buffer.alloc(4 * 1024),
Expand Down Expand Up @@ -1684,7 +1720,26 @@ The server can be a TCP server or an [IPC][] server, depending on what it
Here is an example of a TCP echo server which listens for connections
on port 8124:

```js
```mjs
import net from 'node:net';
const server = net.createServer((c) => {
// 'connection' listener.
console.log('client connected');
c.on('end', () => {
console.log('client disconnected');
});
c.write('hello\r\n');
c.pipe(c);
});
server.on('error', (err) => {
throw err;
});
server.listen(8124, () => {
console.log('server bound');
});
```

```cjs
const net = require('node:net');
const server = net.createServer((c) => {
// 'connection' listener.
Expand Down
Loading