Backend
Node.js
A server-side JavaScript runtime built on the V8 engine with an event-driven, non-blocking I/O model.
What it is
Node.js runs JavaScript outside the browser. Under the hood sits Chrome's V8 engine and the libuv library, which handles networking, files and timers through an event loop.
Your code runs on one thread, but I/O does not block it: while one request waits for the database, the event loop serves others. For heavy computation there are worker_threads and separate processes.
The npm ecosystem is the largest of any language. That made Node.js the base for APIs, BFFs, build tools (Vite, webpack) and server-side rendering.
Use it when
- I/O-heavy services: REST and GraphQL APIs, chats, streaming, websockets.
- The team already writes JavaScript or TypeScript and wants one language on front and back.
- You need build tooling, CLI utilities or a BFF for a frontend.
Avoid it when
- Long CPU-bound jobs (video processing, ML) on the main thread: they block the event loop.
- You need strict static typing without extra tooling: use TypeScript or another language.
Key concepts
- Event loop
- A loop that takes ready callbacks from queues and runs them one at a time on the main thread.
- libuv
- The C library for async I/O and the thread pool underneath Node.
- Streams
- Processing data in chunks without loading the whole file into memory.
- npm / ESM
- The package manager and the modern
import/exportmodule system.
Example
Cheat sheet
| Command or code | What it does |
|---|---|
node app.js | Runs a file |
node --watch app.js | Restarts on file changes |
node --env-file=.env app.js | Loads variables from .env |
npm init -y | Creates package.json |
npm install express | Installs a dependency |
process.env.PORT | Environment variable |
import { readFile } from 'node:fs/promises' | Files through the Promise API |
await readFile('a.json', 'utf8') | Reads a file as text |
node --test | Runs the built in tests |
import path from 'node:path' | Working with paths |
How to learn it
- First take the JavaScript course, especially async and modules.
- Take the Node.js essentials course on ShortLesson.
- Learn modules, the file system, paths and environment variables.
- Understand the event loop and the difference between callbacks, Promises and async/await.
- Write an HTTP server, first with the built in http module, then with Express.
- Connect a database and build a REST API with authentication.
- Add tests with node:test or Vitest.
- Use TypeScript in Node and deploy a service.
Interview questions
1. What is the event loop?
It is the mechanism that takes finished async operations from queues and runs their callbacks on one thread. It lets Node serve thousands of connections without a thread per connection.
2. Why should you not block the main thread?
While synchronous code runs, the event loop is stuck and every other request waits. Heavy computation moves to worker threads or a separate service.
3. How does
process.nextTickdiffer fromsetImmediate?nextTickruns right after the current operation, before the loop moves on, whilesetImmediateruns in the check phase after I/O. Too manynextTickcalls can starve the loop.
Interview questions from the bank
- In Node.js there are two
setTimeoutcalls with zero delay. The first schedulesPromise.thenandqueueMicrotask, the secondPromise.thenandprocess.nextTick. Do the microtasks of the first timer run before the second timer? What goes first:nextTickor a promise?MiddleJavaScript - An endpoint enriches 2000 orders with data from an external service. The version with
awaitin aforloop takes 40 seconds, the version withPromise.allgets 429 and a DB pool timeout. What is wrong with both, and how do you do it right?MiddleJavaScript - The code holds only one
theThing, butheapUsedgrows to hundreds of megabytes, although the helper functionunusedis never called. Why does this happen with closures, and how do you find it in a heap snapshot?SeniorJavaScript - An endpoint merges the request body into a settings object with a recursive function, and after one request all users got admin rights. How does prototype pollution work and how do you close it?SeniorJavaScript
- A colleague says an import loop does not block the server because it has
await. But during the import the health check times out. Why, and how do you fix it?SeniorJavaScript - How does the browser event loop differ from the Node.js event loop? What does the language define and what does the environment define?SeniorJavaScript
- An index by id from 20,000 rows is built with
rows.reduce((acc, r) => ({ ...acc, [r.id]: r }), {})and takes hundreds of milliseconds, while a simple loop is almost instant. Why, and how do you fix it without giving up purity?SeniorJavaScript - An async generator takes a connection from the pool and yields cursor rows, and under load the pool is exhausted. Where does the connection leak and how do you fix it?SeniorJavaScript
Related technologies
- DenoA secure JavaScript and TypeScript runtime from the creator of Node.js, with built-in tooling.
- BunA fast JavaScript and TypeScript runtime that bundles a runtime, package manager, bundler and test runner.
- ExpressA minimalist web framework for Node.js with routing and a middleware chain.
- NestJSA progressive TypeScript Node.js framework with modules, decorators and dependency injection.
- FastifyA fast Node.js framework with a plugin architecture and JSON Schema validation.
- TypeScriptA superset of JavaScript with static types that compiles to plain JS.
Logos belong to their owners and are used for identification only. Icons: Simple Icons (CC0), Devicon (MIT).