Node.js AdvancedLesson 95Free preview
Net Module
This lesson is part of Node.js · All levels
Summary
This lesson discusses the net modules for TCP socket-level network communication at Node.js. You'll create TCP servers and clients with net.createServer and net.connect, then handle event data, end, and error. These modules become the foundation of custom protocols, such as game servers, proxies, or services that require full control over the flow of bytes.
Key points
- Net.createServer makes the TCP server; net.connect(host, port) makes the client.
- Sockets are stream-like: use event 'data' to read and socket.write() to write.
- Event 'end' indicates the client closes the writing side; 'close' indicates the socket is completely closed.
- Since TCP has no message limit, you need to frame it yourself.
- Set error handler because the network can fail at any time (ECONNRESET, etc.).
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.