Node.js v23.6.1 documentation
- Node.js v23.6.1
-
Table of contents
- Net
- IPC support
- Class:
net.BlockList - Class:
net.SocketAddress - Class:
net.Servernew net.Server([options][, connectionListener])- Event:
'close' - Event:
'connection' - Event:
'error' - Event:
'listening' - Event:
'drop' server.address()server.close([callback])server[Symbol.asyncDispose]()server.getConnections(callback)server.listen()server.listeningserver.maxConnectionsserver.dropMaxConnectionserver.ref()server.unref()
- Class:
net.Socketnew net.Socket([options])- Event:
'close' - Event:
'connect' - Event:
'connectionAttempt' - Event:
'connectionAttemptFailed' - Event:
'connectionAttemptTimeout' - Event:
'data' - Event:
'drain' - Event:
'end' - Event:
'error' - Event:
'lookup' - Event:
'ready' - Event:
'timeout' socket.address()socket.autoSelectFamilyAttemptedAddressessocket.bufferSizesocket.bytesReadsocket.bytesWrittensocket.connect()socket.connectingsocket.destroy([error])socket.destroyedsocket.destroySoon()socket.end([data[, encoding]][, callback])socket.localAddresssocket.localPortsocket.localFamilysocket.pause()socket.pendingsocket.ref()socket.remoteAddresssocket.remoteFamilysocket.remotePortsocket.resetAndDestroy()socket.resume()socket.setEncoding([encoding])socket.setKeepAlive([enable][, initialDelay])socket.setNoDelay([noDelay])socket.setTimeout(timeout[, callback])socket.timeoutsocket.unref()socket.write(data[, encoding][, callback])socket.readyState
net.connect()net.createConnection()net.createServer([options][, connectionListener])net.getDefaultAutoSelectFamily()net.setDefaultAutoSelectFamily(value)net.getDefaultAutoSelectFamilyAttemptTimeout()net.setDefaultAutoSelectFamilyAttemptTimeout(value)net.isIP(input)net.isIPv4(input)net.isIPv6(input)
- Net
-
Index
- Assertion testing
- Asynchronous context tracking
- Async hooks
- Buffer
- C++ addons
- C/C++ addons with Node-API
- C++ embedder API
- Child processes
- Cluster
- Command-line options
- Console
- Corepack
- Crypto
- Debugger
- Deprecated APIs
- Diagnostics Channel
- DNS
- Domain
- Errors
- Events
- File system
- Globals
- HTTP
- HTTP/2
- HTTPS
- Inspector
- Internationalization
- Modules: CommonJS modules
- Modules: ECMAScript modules
- Modules:
node:moduleAPI - Modules: Packages
- Modules: TypeScript
- Net
- OS
- Path
- Performance hooks
- Permissions
- Process
- Punycode
- Query strings
- Readline
- REPL
- Report
- Single executable applications
- SQLite
- Stream
- String decoder
- Test runner
- Timers
- TLS/SSL
- Trace events
- TTY
- UDP/datagram
- URL
- Utilities
- V8
- VM
- WASI
- Web Crypto API
- Web Streams API
- Worker threads
- Zlib
- Other versions
- Options
Net#
Source Code: lib/net.js
The node:net module provides an asynchronous network API for creating stream-based
TCP or IPC servers (net.createServer()) and clients
(net.createConnection()).
It can be accessed using:
import net from 'node:net';const net = require('node:net');
IPC support#
The node:net module supports IPC with named pipes on Windows, and Unix domain
sockets on other operating systems.
Identifying paths for IPC connections#
net.connect(), net.createConnection(), server.listen(), and
socket.connect() take a path parameter to identify IPC endpoints.
On Unix, the local domain is also known as the Unix domain. The path is a
file system pathname. It will throw an error when the length of pathname is
greater than the length of sizeof(sockaddr_un.sun_path). Typical values are
107 bytes on Linux and 103 bytes on macOS. If a Node.js API abstraction creates
the Unix domain socket, it will unlink the Unix domain socket as well. For
example, net.createServer() may create a Unix domain socket and
server.close() will unlink it. But if a user creates the Unix domain
socket outside of these abstractions, the user will need to remove it. The same
applies when a Node.js API creates a Unix domain socket but the program then
crashes. In short, a Unix domain socket will be visible in the file system and
will persist until unlinked. On Linux, You can use Unix abstract socket by adding
\0 to the beginning of the path, such as \0abstract. The path to the Unix
abstract socket is not visible in the file system and it will disappear automatically
when all open references to the socket are closed.
On Windows, the local domain is implemented using a named pipe. The path must
refer to an entry in \\?\pipe\ or \\.\pipe\. Any characters are permitted,
but the latter may do some processing of pipe names, such as resolving ..
sequences. Despite how it might look, the pipe namespace is flat. Pipes will
not persist. They are removed when the last reference to them is closed.
Unlike Unix domain sockets, Windows will close and remove the pipe when the
owning process exits.
JavaScript string escaping requires paths to be specified with extra backslash escaping such as:
net.createServer().listen(
path.join('\\\\?\\pipe', process.cwd(), 'myctl'));
Class: net.BlockList#
The BlockList object can be used with some network APIs to specify rules for
disabling inbound or outbound access to specific IP addresses, IP ranges, or
IP subnets.
blockList.addAddress(address[, type])#
address<string> | <net.SocketAddress> An IPv4 or IPv6 address.type<string> Either'ipv4'or'ipv6'. Default:'ipv4'.
Adds a rule to block the given IP address.
blockList.addRange(start, end[, type])#
start<string> | <net.SocketAddress> The starting IPv4 or IPv6 address in the range.end<string> | <net.SocketAddress> The ending IPv4 or IPv6 address in the range.type<string> Either'ipv4'or'ipv6'. Default:'ipv4'.
Adds a rule to block a range of IP addresses from start (inclusive) to
end (inclusive).
blockList.addSubnet(net, prefix[, type])#
net<string> | <net.SocketAddress> The network IPv4 or IPv6 address.prefix<number> The number of CIDR prefix bits. For IPv4, this must be a value between0and32. For IPv6, this must be between0and128.type<string> Either'ipv4'or'ipv6'. Default:'ipv4'.
Adds a rule to block a range of IP addresses specified as a subnet mask.
blockList.check(address[, type])#
address<string> | <net.SocketAddress> The IP address to checktype<string> Either'ipv4'or'ipv6'. Default:'ipv4'.- Returns: <boolean>
Returns true if the given IP address matches any of the rules added to the
BlockList.
const blockList = new net.BlockList();
blockList.addAddress('123.123.123.123');
blockList.addRange('10.0.0.1', '10.0.0.10');
blockList.addSubnet('8592:757c:efae:4e45::', 64, 'ipv6');
console.log(blockList.check('123.123.123.123')); // Prints: true
console.log(blockList.check('10.0.0.3')); // Prints: true
console.log(blockList.check('222.111.111.222')); // Prints: false
// IPv6 notation for IPv4 addresses works:
console.log(blockList.check('::ffff:7b7b:7b7b', 'ipv6')); // Prints: true
console.log(blockList.check('::ffff:123.123.123.123', 'ipv6')); // Prints: true
blockList.rules#
- Type: <string[]>
The list of rules added to the blocklist.
BlockList.isBlockList(value)#
value<any> Any JS value- Returns
trueif thevalueis anet.BlockList.
Class: net.SocketAddress#
new net.SocketAddress([options])#
options<Object>
socketaddress.address#
- Type <string>
socketaddress.family#
- Type