Node.js is a JavaScript runtime built on Chrome's V8 engine that lets you run JavaScript outside the browser, which makes it a common choice for a Node.js web server. You build a simple server by importing the built-in node:http module, creating a server with a request handler, and calling listen on a port. No framework or install step is needed for the basics.
In this article you will write a working server, route requests by URL and method, send JSON, and learn why Node can handle many connections with a single thread. It builds on the basics of what a server does.
Node.js packages the V8 JavaScript engine with a C library called libuv, which handles file, network and timer operations. When your code asks for something slow, such as reading a file or waiting for a database, Node hands the work to the operating system and keeps running other code. When the result is ready, a callback or promise continues the work. This is the event loop.
The result is that one Node process can keep thousands of connections open while it waits on I/O. It is a strong fit for APIs, real-time apps and proxies. It is a weaker fit for long CPU-heavy work such as video encoding, because that blocks the single JavaScript thread. For that kind of work you use worker threads or a separate service.
Check your install first. Use a current LTS release (Node.js 22 or 24 as of late 2026):
Create a file named server.js:
To use import in a .js file, add "type": "module" to your package.json (run npm init -y first), or name the file server.mjs. Start it with:
Open http://localhost:3000 in a browser or run curl http://localhost:3000. You will see the text response.
The handler receives two objects. req is an IncomingMessage with the method, URL and headers. res is a ServerResponse that you use to set the status code and headers and to write the body. Calling res.end() finishes the response; if you forget it, the client waits until it times out.
The handler runs for every request, so you decide what to send based on req.method and req.url. Parse the URL with the standard URL class rather than splitting strings by hand:
Requesting /hello?name=Asha returns:
Always send the right Content-Type and a correct status code. A missing route returns 404, not 200 with an error message.
The body of a POST request arrives as a stream. In modern Node you can read it with for await:
Test it:
Two habits matter here: cap the body size so a client cannot exhaust memory, and wrap JSON.parse in error handling so bad input returns 400 instead of crashing the process.
The raw node:http module is fine for learning and for tiny services, but real apps need routing tables, body parsing, validation, authentication, logging and error handling. Writing all of that by hand with if chains becomes repetitive. Frameworks such as Express, Fastify and Hono sit on top of node:http (or the same ideas) and give you those pieces.
| Approach | Good for | Drawback |
|---|---|---|
node:http alone | Learning, health checks, tiny tools | You write routing and parsing yourself |
| Framework (Express, Fastify) | Real APIs and web apps | Extra dependency to learn and update |
const port = Number(process.env.PORT) || 3000;.server.on('error', ...) so a port that is already in use gives a clear message.SIGTERM by calling server.close() so in-flight requests finish.req.url, headers or the body without checking it.Neither. Node.js is a runtime that executes JavaScript outside the browser. The language is JavaScript, and Express or Fastify are frameworks that run on Node.
The process is tied to your shell. In production run it under a process manager such as systemd, a container, or a platform service that restarts it if it exits.
A single Node process runs your JavaScript on one thread. Use the cluster module, worker threads, or several processes behind a load balancer to use multiple cores.
No. node:http is enough for simple cases, but a framework saves time once you have several routes, middleware and validation.
Node.js runs JavaScript on the server using an event-driven, non-blocking model. A server is a request handler passed to createServer and started with listen. Route by method and path, set correct status codes and content types, parse JSON bodies with a size limit, and move to a framework when the hand-written routing grows.