Node.js Advanced Topics

zhuting
2 min readMay 24, 2022

--

  • OS
  • Process
  • Net
  • UDP
  • HTTP
  • REPL

The os module is a very nifty little module that allows us to get information about the OS.

const os = require('node:os');  
console.log("Architecture:", os.arch(), "\n")
console.log("CPUs:", os.cpus(), "\n")
console.log("Network interfaces:", os.networkInterfaces(), "\n") console.log("Platform:", os.platform(), "\n")
console.log("Release number:", os.release(), "\n")
console.log("User info:", os.userInfo(), "\n")

os.arch() returns the operating system CPU architecture.

The OS module is rarely used, but now you know that it’s available if you need it.

The process object is a global that allows us to control the current process. It also has methods that can provide useful information about the process. Being an instance of the EventEmitter class, it has a few important events that we should know about.

  • process.stdin is a readable stream. We use it to read data from user input.
  • process.stdout is a writable stream. It is written to asynchronously, making it non-blocking. console.log and console.info use this stream.
  • process.stderr is also a writable stream. It is written to synchronously and, hence, is blocking. console.warn and console.error use this stream.

You can read up on more methods in the official documentation.

Understanding the workings of the net module in Node.js

  • TCP

One of the most common transport layer protocols is TCP(Transmission Control Protocol). It provides a reliable way of sending packets in an ordered and error-checked stream. TCP is connection-oriented, which means that a connection must be established between the client and the server before data can be sent.

  • UDP

Alongside TCP, UDP is the other most common communication protocol. It provides a simple and lightweight connectionless communication model. Being connectionless, a very minimal amount of protocol mechanisms come into play when sending packets. While checksums are still used for data integrity, UDP is favored in real-time applications, as it avoids the overhead of setting up a connection, error-checking, and retransmission delays.

--

--