Node’s core APIs are event-driven
Most Node.js I/O objects extend EventEmitter and signal progress and completion
by emitting named events. Knowing each event name, which object emits it, and the
listener’s arguments is essential to wiring up servers, streams and processes.
This reference lists the core built-in events by module.
How it works
You react to an event by registering a listener with the documented signature. The emitter calls every listener synchronously, in order, with the event’s payload arguments:
server.on("request", (req, res) => res.end("ok"));
readable.on("data", (chunk) => total += chunk.length);
readable.on("end", () => console.log("done"));
readable.on("error", (err) => console.error(err)); // always attach
child.on("exit", (code, signal) => console.log(code, signal));
Use on for repeated events and once for one-shot events. The error event is
special: an unhandled error throws and crashes the process.
Key events by module
http.Server
request— emitted for each incoming request; listener receives(req: IncomingMessage, res: ServerResponse). The main handler for HTTP servers.connection— a new TCP socket has been established, before any request is parsed.close— the server has stopped accepting connections (afterserver.close()drains existing connections).
Readable streams (fs.createReadStream, http.IncomingMessage, etc.)
data— a chunk is available; emitted only in flowing mode. Listener receives(chunk: Buffer | string).end— no more data will be provided; the read side is finished.error— an error occurred reading; always handle this or the process may crash.close— the underlying file descriptor or socket has been released.
Writable streams (fs.createWriteStream, http.ServerResponse, etc.)
drain— the internal buffer has been flushed and the stream can accept more writes; important whenwrite()returnsfalse.finish— all data has been flushed to the underlying system after callingend().error— write error occurred.
net.Socket
connect— connection established.data— data received from the remote end.end— the other end has signalled it has no more data to send (half-close).close— socket is fully closed; receives(hadError: boolean).
child_process.ChildProcess
spawn— child process has started successfully.exit— child process terminated; listener receives(code: number | null, signal: NodeJS.Signals | null).close— bothexithas fired AND all stdio streams have closed. Use this when you need to know all output has been captured.error— the process could not be spawned, killed, or sent a message.
process
exit— process is about to exit; listener receives(code: number). Only synchronous operations here.uncaughtException— an exception was not caught; log and exit cleanly.unhandledRejection— a Promise rejection was not handled; listener receives(reason, promise).SIGINT/SIGTERM— signals for graceful shutdown (Ctrl+C, container stop).
Tips and notes
- Always attach an
errorlistener to every stream, socket and server before use. datais emitted only in flowing mode — calling.on("data")switches a readable stream into flowing mode.- For child processes, wait for
close(not justexit) when you must be sure all stdout/stderr has been flushed. - Removing listeners with
removeListener/offprevents leaks on long-lived emitters; watch for theMaxListenersExceededWarning.