GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — doc: add esm examples to node:net · nodejs/node@1ad659a

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • doc/api
    • net.md
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎doc/api/net.md‎

Copy file name to clipboardExpand all lines: doc/api/net.md
+60-5Lines changed: 60 additions & 5 deletions
  • Display the source diff
  • Display the rich diff
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

@@ -1514,7 +1518,23 @@ Additional options:
15141518
Following is an example of a client of the echo server described
15151519
in the [`net.createServer()`][] section:
15161520

1517-
```js
1521+
```mjs
1522+
import net from 'node:net';
1523+
const client = net.createConnection({ port: 8124 }, () => {
1524+
// 'connect' listener.
1525+
console.log('connected to server!');
1526+
client.write('world!\r\n');
1527+
});
1528+
client.on('data', (data) => {
1529+
console.log(data.toString());
1530+
client.end();
1531+
});
1532+
client.on('end', () => {
1533+
console.log('disconnected from server');
1534+
});
1535+
```
1536+
1537+
```cjs
15181538
const net = require('node:net');
15191539
const client = net.createConnection({ port: 8124 }, () => {
15201540
// 'connect' listener.
@@ -1541,10 +1561,26 @@ option. In this case, the `onread` option will be only used to call
15411561
`new net.Socket([options])` and the `port` option will be used to
15421562
call `socket.connect(options[, connectListener])`.
15431563

1544-
```js
1564+
```mjs
1565+
import net from 'node:net';
1566+
import { Buffer } from 'node:buffer';
1567+
net.createConnection({
1568+
port: 8124,
1569+
onread: {
1570+
// Reuses a 4KiB Buffer for every read from the socket.
1571+
buffer: Buffer.alloc(4 * 1024),
1572+
callback: function(nread, buf) {
1573+
// Received data is available in `buf` from 0 to `nread`.
1574+
console.log(buf.toString('utf8', 0, nread));
1575+
},
1576+
},
1577+
});
1578+
```
1579+
1580+
```cjs
15451581
const net = require('node:net');
15461582
net.createConnection({
1547-
port: 80,
1583+
port: 8124,
15481584
onread: {
15491585
// Reuses a 4KiB Buffer for every read from the socket.
15501586
buffer: Buffer.alloc(4 * 1024),
@@ -1665,7 +1701,26 @@ The server can be a TCP server or an [IPC][] server, depending on what it
16651701
Here is an example of a TCP echo server which listens for connections
16661702
on port 8124:
16671703

1668-
```js
1704+
```mjs
1705+
import net from 'node:net';
1706+
const server = net.createServer((c) => {
1707+
// 'connection' listener.
1708+
console.log('client connected');
1709+
c.on('end', () => {
1710+
console.log('client disconnected');
1711+
});
1712+
c.write('hello\r\n');
1713+
c.pipe(c);
1714+
});
1715+
server.on('error', (err) => {
1716+
throw err;
1717+
});
1718+
server.listen(8124, () => {
1719+
console.log('server bound');
1720+
});
1721+
```
1722+
1723+
```cjs
16691724
const net = require('node:net');
16701725
const server = net.createServer((c) => {
16711726
// 'connection' listener.

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.