Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท HTTP, HTTPS, net, TLS, dgram and dns

Node.js net.createServer() method

Reference for net.createServer() in Node.js: create a TCP or IPC server, handle connections, set options such as allowHalfOpen and noDelay, and listen on a port.

net.createServer() creates a TCP server (or a server for local IPC, see Unix domain sockets). It returns a net.Server object that is not yet listening; you call server.listen() to start accepting connections. Every time a client connects, the server creates a net.Socket for that client and passes it to your connection listener.

TCP gives you an ordered, reliable stream of bytes with no message boundaries. A single write() on one side can arrive as several data events on the other, and several writes can arrive as one. Anything that needs messages (lines, length-prefixed frames) has to add its own framing on top.

NODE
import net from 'node:net';

const server = net.createServer((socket) => {
  socket.end('hello\n');
});
server.listen(3000, '127.0.0.1');

Try it

Node.js An echo server and a client

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js Counting connections and the listener form

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Syntax

NODE
net.createServer(options?: object, connectionListener?: (socket: net.Socket) => void): net.Server
Name Description
options.allowHalfOpen Default false: when the client ends its side, the server socket ends automatically. With true you must call socket.end() yourself, which allows a response after the client has finished sending.
options.pauseOnConnect Default false. With true, each incoming socket starts paused, so no data is read until you call socket.resume(). Useful when handing the socket to another process.
options.noDelay Default false. With true, Nagle's algorithm is disabled on each accepted socket (setNoDelay(true)), so small writes are sent immediately.
options.keepAlive Default false. Enables TCP keep-alive probes on each accepted socket.
options.keepAliveInitialDelay Delay in milliseconds before the first keep-alive probe. Default 0.
options.highWaterMark Overrides the readable and writable highWaterMark of new sockets.
options.blockList A net.BlockList whose addresses are refused on connect.
connectionListener Automatically added as a listener for the 'connection' event.

Returns: a net.Server.

Errors and edge cases: an invalid option type throws ERR_INVALID_ARG_TYPE synchronously. Problems such as a port already in use do not throw from createServer(); they arrive later as an 'error' event on the server (usually EADDRINUSE or EACCES).

Usage notes

  • Creating is not listening. Nothing happens on the network until server.listen(). Listen on port 0 in tests and read the real port from server.address().
  • Always handle 'error' on the server and on every socket. An unhandled 'error' event throws and can crash the process; a client that resets the connection (ECONNRESET) is normal on the internet.
  • Bind to 127.0.0.1 for local-only services. Listening without a host binds to all interfaces (:: or 0.0.0.0), which exposes the service to the network.
  • Streams, not messages. Use setEncoding() carefully for multi-byte text (it handles split characters), and add framing (newline-delimited, length prefix) for protocols.
  • Backpressure matters. socket.write() returns false when the buffer is full; wait for 'drain' before writing more, or use pipeline().
  • Graceful shutdown. server.close() stops accepting new connections but waits for existing ones to end. net.Server has no "close everything" method, so keep a Set of open sockets yourself if you need to force them closed.
  • Use net for custom protocols only. For HTTP use http.createServer(); for encryption use tls.createServer().