Node.js follows an event-driven architecture, where actions in the system trigger events, and specific functions (listeners) respond to those events.
An event is a signal that something has happened, such as:
A file finishing loading
A request reaching a server
A timer completing
Node.js reacts to these events instead of waiting for tasks to finish.
Node.js provides the events module, which contains the EventEmitter class used to create and handle custom events.
| Method | Description |
|---|---|
.on() | Register an event listener |
.emit() | Trigger an event |
.once() | Listen only once |
.removeListener() | Remove a listener |
.removeAllListeners() | Remove all listeners |
Error events must be handled explicitly.
✔ Multiple listeners can respond to the same event.
http – request and response events
fs – file read/write events
stream – data and end events
process – system-level events
Efficient handling of multiple tasks
Non-blocking execution
Highly scalable
Cleaner separation of logic
Avoid too many listeners on one event
Always handle 'error' events
Use meaningful event names
Remove unused listeners
Node.js is built around events
Events are handled using EventEmitter
Events allow asynchronous, non-blocking code
Used extensively in Node.js core modules
Take quizzes related to this topic and see where you stand!
Start Quiz Now