Node.js
Node.js lets you run JavaScript without a web browser. Developers use it to build web servers, command-line tools, and scripts.
Also known as: Node, нода
What Node.js provides
Run node app.js and your JavaScript program starts as an operating system process. The V8 engine runs the language, while Node.js provides APIs for tasks such as reading files, opening network connections, and starting child processes. Browser JavaScript uses the same language, but a Node.js program doesn't get a web page or a DOM to manipulate. Node.js is a runtime, not a framework: you can build a server with its standard library or add a framework such as Express or Fastify for routing and other application-level features.
How one server handles overlapping requests
Suppose one request needs to read a file and another just needs to return a status code. If the first handler starts an asynchronous read and waits for its result, the second handler can run before the read finishes. When the file is ready, the first handler can resume. The requests overlap even though their JavaScript handlers don't run in parallel on the main thread.
Node.js uses libuv to run the event loop and coordinate asynchronous I/O. Network sockets generally rely on the operating system's I/O facilities, while operations such as asynchronous file reads typically use libuv's thread pool. That's why saying “Node.js is single-threaded” is misleading: JavaScript handlers run one at a time on the main thread, but not all work in the process happens there.
Example: serve a file over HTTP
Save this as server.mjs and start it with node server.mjs. Open http://127.0.0.1:3000 in a browser. The server reads message.txt from the directory you launched it in every time it gets a request.
import { createServer } from 'node:http';
import { readFile } from 'node:fs/promises';
createServer(async (req, res) => {
try {
const text = await readFile('message.txt', 'utf8');
res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
res.end(text);
} catch {
res.writeHead(500);
res.end('Could not read message.txt');
}
}).listen(3000, '127.0.0.1');
While the read is pending, the main thread can handle other requests. If the file doesn't exist, the rejected promise is caught and the server sends an HTTP 500 response.
Where it fits and what can go wrong
Node.js is a common choice for HTTP APIs and WebSocket services that spend much of their time waiting on databases or networks. It also runs test runners, build tools, and command-line utilities. In a server, replacing readFile with readFileSync would make the main thread wait for the disk. A long computation has a similar effect: other requests have to wait even if their own work is trivial. For CPU-heavy tasks, consider worker threads, a separate process, or a background service.
In interviews
You may be asked why adding async to a slow handler doesn't fix response times. Explain which parts wait for I/O and which parts actually run JavaScript. await suspends the function while a promise is pending; it does not make a large calculation or a synchronous API call non-blocking.