What is Express and what problem does it solve on top of Node's `http` module?
Show answer — try answering out loud first
Express is a minimalist web framework for Node.js built on top of Node's http module. It simplifies the work of creating servers: it gives you a clean routing system, middleware support, and a more convenient API for handling the req (request) and res (response) objects. It's not opinionated: it doesn't force you into a folder structure or a way of organizing the project.
With Node's http module you can create a server, but you have to do everything by hand: check req.method and req.url with if/else, parse the URL, write the headers, etc. It's verbose and repetitive.
Express puts a convenient layer on top of that:
- Simple routing: instead of a giant
if, you writeapp.get("/ruta", ...),app.post(...), etc. - Middleware: functions that run in a chain for each request (logs, authentication, body parsing...).
- Helpers on
reqandres: methods likeres.json(),res.status(), orreq.paramsthat save you code. - Not opinionated: Express doesn't impose a structure. You decide how to organize folders, which database to use, and how to split the code. It's flexible, unlike "opinionated" frameworks like Nest or Rails.
Key idea: Express doesn't replace the http module, it wraps it. Underneath it's still the same Node server.
// We import Express (after installing it with: npm install express)
const express = require("express");
// We create the application: 'app' is our Express server
const app = express();
// We define a route: when a GET arrives at "/", we respond with text
app.get("/", (req, res) => {
res.send("¡Hola desde Express!");
});
// Another route that responds with JSON
app.get("/estado", (req, res) => {
res.json({ ok: true, mensaje: "El servidor está vivo" });
});
// We set the server to listen on port 3000
app.listen(3000, () => {
console.log("Servidor escuchando en http://localhost:3000");
});
For comparison, this is how the same thing would look without Express, using only the http module:
const http = require("http");
// We have to check method and url by hand, and write headers manually
const server = http.createServer((req, res) => {
if (req.method === "GET" && req.url === "/") {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("¡Hola desde Node puro!");
} else {
res.writeHead(404);
res.end("No encontrado");
}
});
server.listen(3000);
Thinking that Express is Node or that it replaces the HTTP server. Express is just a library (dependency) that runs on Node and uses its http module underneath. That's why you have to install it (npm install express), whereas http already comes included with Node. Another frequent mistake is forgetting to call app.listen(...): without that, the server never starts and the routes don't respond.
"Express is a minimalist web framework for Node that runs on top of the
httpmodule. It doesn't replace it, it wraps it to give you a more convenient API. Without Express you'd have to checkreq.methodandreq.urlwith a bunch ofifs, parse everything by hand, and write the headers yourself. Express gives you routing withapp.get,app.post, etc., middleware support, and helpers likeres.json. It's also not opinionated: it doesn't impose a folder structure or a way of organizing the project, you decide. A minimal server is three steps: create the app withexpress(), define routes withapp.get, and start it withapp.listen."
Why do we say Express is "not opinionated", and mention one reason why you need to run npm install express but you don't need to install http?
See answer
We say Express is not opinionated because it doesn't force you into a folder structure, an architecture pattern, or a specific database: you decide how to organize everything. "Opinionated" frameworks (like Nest or Rails) do impose conventions.
You need npm install express because Express is an external dependency (a third-party library that doesn't come with Node). By contrast, http is a native module that already comes included with Node, which is why you just require("http") it without installing anything.