Node lets JavaScript run outside the browser. Express is a tiny framework that makes building a web server easy.
You already know JavaScript. Node.js is a program that runs JavaScript on a computer instead of in a browser — so you can use the same language to build servers. That's a superpower: one language, whole app.
# you write a file server.js, then run it with Node:
node server.js
# it keeps running, listening for requests
Raw Node can make a server, but it's clunky. Express is the standard framework that makes it clean:
const express = require("express"); // 1. bring in Express
const app = express(); // 2. make an app
app.get("/", (req, res) => { // 3. when someone visits "/"
res.send("Hello from the server!"); // send this back
});
app.listen(4000, () => { // 4. start listening on port 4000
console.log("Server running on http://localhost:4000");
});
/, run this function and send a reply. Start listening on port 4000." That's a real, working web server. Everything else is just more routes.| Object | Is | You use it to… |
|---|---|---|
req (request) | what the client sent | read the URL, the body (req.body), params (req.params.id) |
res (response) | what you send back | reply with data: res.json(notes), res.status(404) |
(req, res) => { ... }: read what came in from req, send an answer with res. Master those two and you've mastered Express.mkdir notes-server && cd notes-server
npm init -y # create a package.json
npm install express # download Express
node server.js # run it → "Server running on http://localhost:4000"
app.get("/", (req, res) => ...), what is res for?res is the response — res.json(...), res.send(...) send data back.