Overview
Recently I've developed a project using Node.js in my workplace. For deeper understanding, and of course my curiosity, I'd like to read Node.js api documentation. The purpose is to look around apis briefly.
List of APIs
Here is an official documentation.
APIs
node:assert
This API seems to be mainly for testing. I wondered if I could use it instead of an if statement, but it looks like I can't. assert doesn't control the flow of execution — it just throws an error when a condition fails.
import { strict as assert } from 'node:assert';
assert.deepEqual([[[1, 2, 3]], 4, 5], [[[1, 2, 3]], 4, 5]); //pass
assert.deepEqual([[[1, 2, 3]], 4, 5], [[[1, 2, '3']], 4, 5]); //fail
node:async_hooks
This API provides the AsyncLocalStorage
and AsyncResource
classes, which are used to handle asynchronous data and events.
There's also a lower-level async_hooks API, but it's still experimental and not recommended for production use.
node:buffer
This module provides the Blob, Buffer, and File classes, along with some additional module APIs and constants. Personally, the File class feels a little out of place here.
C++ addons
I don't know C or C++ very well, but it seems you can write Node.js addons using those languages.
C++ embedder API
This allows you to run JavaScript in a Node.js environment from C++. But... when would you actually need to do that? Maybe when you're building a C++ library for Node.js? Rference
node:child_process
This module lets you spawn subprocesses, similar to the popen()
function in Linux. What is popen?
According to the man page, The popen() function opens a process by creating a pipe, forking, and invoking the shell.
When would you use this API?
Reference
Reference
node:cluster
Cluster! I'm really curious about this module because software architecture is super interesting. I guess this API is related to that.
Internally, it uses child_process to implement clustering. Both cluster and worker_threads are used for optimization. While the cluster API can help scale a web app across multiple CPU cores, if you're handling heavy computational workloads, you'll probably want to use worker_threads instead.
Reference