What is Node.js and how does it work?
Node.js is a JavaScript runtime built on Google's V8 engine that lets you run JavaScript outside the browser — primarily on the server. It was created by Ryan Dahl in 2009 to handle high-concurrency I/O workloads efficiently.
V8 compiles and executes JavaScript into machine code.
libuv provides the event loop and handles async I/O via OS-level APIs (epoll, kqueue, IOCP).
Node uses a single-threaded, non-blocking I/O model based on the Reactor Pattern — instead of spawning a thread per request, it registers I/O operations with the OS and reacts to events when they're ready.
CPU-heavy or blocking operations (filesystem, DNS, crypto) are offloaded to the libuv thread pool, keeping the main thread free.
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello from Node.js');
});
server.listen(3000, () => {
console.log('Server running on port 3000');
});This creates an HTTP server using Node.js built-in http module. The createServer method takes a callback that runs for every incoming request. res.writeHead sets the status code and headers, res.end sends the response body and closes the connection.
The server listens on port 3000. This demonstrates how minimal code is needed to create a working server in Node.js.
Interviewers want to hear more than 'it runs JavaScript on the server.' Mention V8, the event-driven non-blocking I/O model, libuv, and what types of applications Node.js excels at. Bonus points for mentioning what it is NOT good at (CPU-bound tasks).