What Node.js is
Browsers include a JavaScript engine. Node.js takes Chrome's engine (V8) and adds what a program on a computer needs: files, network connections and the command line. There is no document or window in Node, because there is no web page.
After installing Node from nodejs.org, save this as hello.js and run node hello.js in a terminal:
console.log("Hello from Node.js");
console.log("Node version:", process.version);Modules
Node comes with built-in modules. You load one with require (or import in newer projects):
const os = require("os");
const path = require("path");
console.log("Platform:", os.platform());
console.log(path.join("projects", "school", "notes.txt"));
path.join builds a file path with the right separator for the operating system, so the same code works on Windows, macOS and Linux.
Reading and writing files
const fs = require("fs");
fs.writeFileSync("scores.txt", "Arta,92\nDren,85\nBora,78\n");
const text = fs.readFileSync("scores.txt", "utf8");
const rows = text.trim().split("\n").map(line => line.split(","));
const best = rows.reduce((a, b) => (Number(b[1]) > Number(a[1]) ? b : a));
console.log(rows.length, "students");
console.log("Top score:", best[0], best[1]);
The Sync functions wait until the file work is finished, which keeps small scripts simple. In a server, prefer the asynchronous versions (fs.promises.readFile with await) so one slow file does not block every visitor.
A tiny web server
The built-in http module can answer requests from a browser. Save this as server.js, run node server.js and open http://localhost:3000:
const http = require("http");
const server = http.createServer((req, res) => {
if (req.url === "/api/time") {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ time: new Date().toISOString() }));
return;
}
if (req.url === "/") {
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
res.end("<h1>Hello from my server</h1><p>Try /api/time</p>");
return;
}
res.writeHead(404, { "Content-Type": "text/plain" });
res.end("Not found");
});
server.listen(3000, () => console.log("Open http://localhost:3000"));
Every request calls the function with req (what the browser asked for) and res (what you send back). The status codes are the same ones explained in how the web works. Stop the server with Ctrl+C.
Packages with npm
Node includes npm, a tool for installing packages other people have written. npm init -y creates a package.json file that lists your project's packages, and npm install express adds the popular Express web framework. Install only packages you need from authors you trust: every package is code that runs on your computer.
Try it yourself
Extend the server with a /api/hello?name=Arta route that answers {"message":"Hello, Arta"}. Hint: new URL(req.url, "http://localhost").searchParams.get("name") reads the name. You can practise the file example in Unays Code while signed in, which runs JavaScript in a sandbox on our server.
This tutorial was written with AI assistance and reviewed by Unays. The runnable code examples were tested before publishing. Spotted a mistake? Tell us at [email protected].