Skip to content

Commit 61b9ed3

Browse files
mfdebiantargos
authored andcommitted
doc: add esm examples to node:net
PR-URL: #55134 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent e361951 commit 61b9ed3

File tree

1 file changed

+60
-5
lines changed

1 file changed

+60
-5
lines changed

doc/api/net.md

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ TCP or [IPC][] servers ([`net.createServer()`][]) and clients
1414

1515
It can be accessed using:
1616

17-
```js
17+
```mjs
18+
import net from 'node:net';
19+
```
20+
21+
```cjs
1822
const net = require('node:net');
1923
```
2024

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

1534-
```js
1538+
```mjs
1539+
import net from 'node:net';
1540+
const client = net.createConnection({ port: 8124 }, () => {
1541+
// 'connect' listener.
1542+
console.log('connected to server!');
1543+
client.write('world!\r\n');
1544+
});
1545+
client.on('data', (data) => {
1546+
console.log(data.toString());
1547+
client.end();
1548+
});
1549+
client.on('end', () => {
1550+
console.log('disconnected from server');
1551+
});
1552+
```
1553+
1554+
```cjs
15351555
const net = require('node:net');
15361556
const client = net.createConnection({ port: 8124 }, () => {
15371557
// 'connect' listener.
@@ -1558,10 +1578,26 @@ option. In this case, the `onread` option will be only used to call
15581578
`new net.Socket([options])` and the `port` option will be used to
15591579
call `socket.connect(options[, connectListener])`.
15601580

1561-
```js
1581+
```mjs
1582+
import net from 'node:net';
1583+
import { Buffer } from 'node:buffer';
1584+
net.createConnection({
1585+
port: 8124,
1586+
onread: {
1587+
// Reuses a 4KiB Buffer for every read from the socket.
1588+
buffer: Buffer.alloc(4 * 1024),
1589+
callback: function(nread, buf) {
1590+
// Received data is available in `buf` from 0 to `nread`.
1591+
console.log(buf.toString('utf8', 0, nread));
1592+
},
1593+
},
1594+
});
1595+
```
1596+
1597+
```cjs
15621598
const net = require('node:net');
15631599
net.createConnection({
1564-
port: 80,
1600+
port: 8124,
15651601
onread: {
15661602
// Reuses a 4KiB Buffer for every read from the socket.
15671603
buffer: Buffer.alloc(4 * 1024),
@@ -1684,7 +1720,26 @@ The server can be a TCP server or an [IPC][] server, depending on what it
16841720
Here is an example of a TCP echo server which listens for connections
16851721
on port 8124:
16861722

1687-
```js
1723+
```mjs
1724+
import net from 'node:net';
1725+
const server = net.createServer((c) => {
1726+
// 'connection' listener.
1727+
console.log('client connected');
1728+
c.on('end', () => {
1729+
console.log('client disconnected');
1730+
});
1731+
c.write('hello\r\n');
1732+
c.pipe(c);
1733+
});
1734+
server.on('error', (err) => {
1735+
throw err;
1736+
});
1737+
server.listen(8124, () => {
1738+
console.log('server bound');
1739+
});
1740+
```
1741+
1742+
```cjs
16881743
const net = require('node:net');
16891744
const server = net.createServer((c) => {
16901745
// 'connection' listener.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy