- Assertion Testing
- Buffer
- C/C++ Addons
- Child Processes
- Cluster
- Command Line Options
- Console
- Crypto
- Debugger
- DNS
- Domain
- Errors
- Events
- File System
- Globals
- HTTP
- HTTPS
- Modules
- Net
- OS
- Path
- Process
- Punycode
- Query Strings
- Readline
- REPL
- Stream
- String Decoder
- Timers
- TLS/SSL
- TTY
- UDP/Datagram
- URL
- Utilities
- V8
- VM
- ZLIB
Node.js v6.11.1 Documentation
Table of Contents
- Net
- Class: net.Server
- Event: 'close'
- Event: 'connection'
- Event: 'error'
- Event: 'listening'
- server.address()
- server.close([callback])
- server.connections
- server.getConnections(callback)
- server.listen(handle[, backlog][, callback])
- server.listen(options[, callback])
- server.listen(path[, backlog][, callback])
- server.listen([port][, hostname][, backlog][, callback])
- server.listening
- server.maxConnections
- server.ref()
- server.unref()
- Class: net.Socket
- new net.Socket([options])
- Event: 'close'
- Event: 'connect'
- Event: 'data'
- Event: 'drain'
- Event: 'end'
- Event: 'error'
- Event: 'lookup'
- Event: 'timeout'
- socket.address()
- socket.bufferSize
- socket.bytesRead
- socket.bytesWritten
- socket.connect(options[, connectListener])
- socket.connect(path[, connectListener])
- socket.connect(port[, host][, connectListener])
- socket.connecting
- socket.destroy([exception])
- socket.destroyed
- socket.end([data][, encoding])
- socket.localAddress
- socket.localPort
- socket.pause()
- socket.ref()
- socket.remoteAddress
- socket.remoteFamily
- socket.remotePort
- socket.resume()
- socket.setEncoding([encoding])
- socket.setKeepAlive([enable][, initialDelay])
- socket.setNoDelay([noDelay])
- socket.setTimeout(timeout[, callback])
- socket.unref()
- socket.write(data[, encoding][, callback])
- net.connect(options[, connectListener])
- net.connect(path[, connectListener])
- net.connect(port[, host][, connectListener])
- net.createConnection(options[, connectListener])
- net.createConnection(path[, connectListener])
- net.createConnection(port[, host][, connectListener])
- net.createServer([options][, connectionListener])
- net.isIP(input)
- net.isIPv4(input)
- net.isIPv6(input)
- Class: net.Server
Net#
Stability: 2 - Stable
The net module provides you with an asynchronous network wrapper. It contains
functions for creating both servers and clients (called streams). You can include
this module with require('net');.
Class: net.Server#
This class is used to create a TCP or local server.
net.Server is an EventEmitter with the following events:
Event: 'close'#
Emitted when the server closes. Note that if connections exist, this event is not emitted until all connections are ended.
Event: 'connection'#
- <net.Socket> The connection object
Emitted when a new connection is made. socket is an instance of
net.Socket.
Event: 'error'#
Emitted when an error occurs. Unlike net.Socket, the 'close'
event will not be emitted directly following this event unless
server.close() is manually called. See the example in discussion of
server.listen().
Event: 'listening'#
Emitted when the server has been bound after calling server.listen.
server.address()#
Returns the bound address, the address family name, and port of the server
as reported by the operating system.
Useful to find which port was assigned when getting an OS-assigned address.
Returns an object with port, family, and address properties:
{ port: 12346, family: 'IPv4', address: '127.0.0.1' }
Example:
const server = net.createServer((socket) => {
socket.end('goodbye\n');
}).on('error', (err) => {
// handle errors here
throw err;
});
// grab a random port.
server.listen(() => {
console.log('opened server on', server.address());
});
Don't call server.address() until the 'listening' event has been emitted.
server.close([callback])#
Stops the server from accepting new connections and keeps existing
connections. This function is asynchronous, the server is finally
closed when all connections are ended and the server emits a 'close' event.
The optional callback will be called once the 'close' event occurs. Unlike
that event, it will be called with an Error as its only argument if the server
was not open when it was closed.
server.connections#
Stability: 0 - Deprecated: Use server.getConnections() instead.The number of concurrent connections on the server.
This becomes null when sending a socket to a child with
child_process.fork(). To poll forks and get current number of active
connections use asynchronous server.getConnections instead.
server.getConnections(callback)#
Asynchronously get the number of concurrent connections on the server. Works when sockets were sent to forks.
Callback should take two arguments err and count.
server.listen(handle[, backlog][, callback])#
handle<Object>backlog<Number>callback<Function>
The handle object can be set to either a server or socket (anything
with an underlying _handle member), or a {fd: <n>} object.