QA
Intermediate LevelNode JS

How do you build custom TCP servers and clients using the node:net module?

❓ Interview Prompt: Explain `node:net` module, `net.createServer()`, `net.connect()`, TCP Socket streams, data events, and custom socket protocols.

The 'node:net' module provides low-level networking capabilities for creating raw TCP servers and socket streams:

•
'net.createServer(socket => ...)': Listens for incoming TCP socket connections.
•
'socket': A Duplex stream handling bidirectional TCP communication.

Code Implementation Example

javascript
import net from 'node:net';

// TCP Server
const server = net.createServer((socket) => {
  console.log('Client connected:', socket.remoteAddress);
  socket.write('Welcome to raw TCP Server!
');

  socket.on('data', (data) => {
    console.log('Received: ${data.toString().trim()}');
    socket.write('Echo: ${data}');
  });
});
server.listen(9000, () => console.log('TCP Server listening on port 9000'));
💡Creating a raw TCP server using node:net.
🎯 Interview Key Takeaway

Use node:net for low-level TCP socket programming when building custom network protocols.

#net#TCP#Sockets#Networking#Intermediate
Back to all Node JS Questions→

More Node JS Interview Questions

Beginner LevelNode JS

How does the Node.js Event Loop work? Explain the 6 execution phases (Timers, Pending, Poll, Check, Close).

Beginner LevelNode JS

What does 'Single-Threaded, Event-Driven, Non-Blocking I/O' mean in Node.js?

Beginner LevelNode JS

What is the difference between process.nextTick() and setImmediate()?