Express Explained: Node's Minimal Web Framework Guide

You need a small shop API. Three routes, a bit of JSON in, a bit of JSON out. Plain Node can do all of that, but you will spend the first hour writing the parts that have nothing to do with your shop: splitting the URL by hand, reading the request body, and deciding what a 404 should look like. That is the problem Express solves, and it solves it by staying small. It gives you routing, request handling, and middleware, then gets out of the way. There is no built in database layer and no required folder layout, so you add the pieces you need, one at a time.

Quick answer

Express is a small Node.js web framework that adds routing and middleware to the built-in HTTP server, so a team can build a JSON API or a small web service without choosing a large framework. Pick it when the service is small, the data is JSON, and the team already works in JavaScript.

const express = require("express");
const app = express();
app.use(express.json());

const orders = [
  { id: 1, item: "Notebook", qty: 2, price: 4.5, paid: true },
  { id: 2, item: "Pen set", qty: 1, price: 9.99, paid: false },
  { id: 3, item: "Desk lamp", qty: 1, price: 24.0, paid: true },
];

app.get("/orders", (req, res) => {
  const paid = req.query.paid === "true";
  res.json(req.query.paid ? orders.filter((o) => o.paid === paid) : orders);
});

app.get("/orders/:id", (req, res) => {
  const order = orders.find((o) => o.id === Number(req.params.id));
  if (!order) return res.status(404).json({ error: "Order not found" });
  res.json(order);
});

app.post("/orders", (req, res) => {
  const { item, qty, price } = req.body;
  if (!item || !qty || !price) {
    return res.status(400).json({ error: "item, qty and price are required" });
  }
  const order = { id: orders.length + 1, item, qty, price, paid: false };
  orders.push(order);
  res.status(201).json(order);
});

app.use((req, res) => res.status(404).json({ error: "Route not found" }));

app.listen(3000, () => console.log("Listening on http://localhost:3000"));

What I would check before using it

My judgement, from building and reviewing Node services for years, is this: pick Express when your service is mostly routes plus a few shared concerns, like JSON parsing, logging, and authentication checks. It is at its best for small to medium APIs, internal tools, and the backend behind a single page app, where the team wants control and the framework should not impose a shape. The middleware model is the real product here. A request passes through a chain of small functions, each doing one job, and that chain is easy to read, easy to test in pieces, and easy to explain to a new teammate.

I would also weigh what Express does not give you. There is no standard way to structure a large app, so two teams will organise the same project differently, and a big codebase can drift into a pile of route files with shared helpers nobody owns. Validation, authentication, and database access are all your choice, which means they are also your responsibility. If your team wants conventions handed to them, or if the service will grow into dozens of modules with background jobs and scheduled work, a more opinionated framework earns its weight.

The verdict, in short: for an API you can describe on one page, Express is the fastest honest choice. The recommendation flips when the project needs strong conventions, built in validation, or a team that will change often and needs the framework to answer the structural questions. In that case, pick the opinionated framework and accept its learning curve, because the structure is the feature you are buying.

A worked example that is not a toy

The example above is a real order API for a small stationery shop, with three orders in memory. It lists orders, filters them by whether they were paid, fetches one by id, and creates a new one. I ran it with Node v24.22.1 and Express 5.3.0, then checked every route with curl. Listing all orders returns the full array. Filtering with `?paid=true` returns only the two paid orders. Fetching order 2 returns the pen set, and asking for order 99 returns a 404 with a JSON error instead of an HTML error page.

$ curl -s "http://localhost:3000/orders?paid=true"
[{"id":1,"item":"Notebook","qty":2,"price":4.5,"paid":true},{"id":3,"item":"Desk lamp","qty":1,"price":24,"paid":true}]

$ curl -s -w "\nHTTP %{http_code}\n" http://localhost:3000/orders/99
{"error":"Order not found"}
HTTP 404

$ curl -s -w "\nHTTP %{http_code}\n" -X POST -H "Content-Type: application/json" -d '{"item":"Sticky notes","qty":3,"price":2.5}' http://localhost:3000/orders
{"id":4,"item":"Sticky notes","qty":3,"price":2.5,"paid":false}
HTTP 201

Creating an order with a missing field returns a 400 with a plain message, and an unknown route returns a 404, because the catch all handler sits at the end. A missing field is worth pausing on. The POST route checks for item, quantity, and price before it creates anything, and answers 400 when one is absent, so a broken client learns what is wrong from the response instead of discovering a half created order later. The status codes matter as much as the bodies: 201 says a resource was created, 400 says the request was wrong, and 404 says the thing does not exist. Clients, and the people debugging them at midnight, rely on those codes more than on any message text. Now the part that goes wrong. If you leave out the line `app.use(express.json())`, the POST route still runs, but `req.body` is undefined, and the server answers with a 500 and an HTML error page:

TypeError: Cannot destructure property 'item' of 'req.body' as it is undefined.
HTTP 500

The fix is one line, added before the routes: `app.use(express.json())`. Express does not parse JSON bodies by itself. That middleware reads the body, parses it, and puts the result on `req.body`, and without it every POST handler is working with nothing. The lesson is general: when an Express handler sees undefined where it expects data, check the middleware first, not the handler.

Common mistakes and how to fix them

1. Forgetting `express.json()`. The symptom is a 500 with `TypeError: Cannot destructure property 'item' of 'req.body' as it is undefined`, or `req.body` logging as undefined. The fix is `app.use(express.json())` before any route that reads a body, and the same applies to `express.urlencoded()` for form posts.

2. Registering the 404 handler before the real routes. Express runs middleware in the order you register it, so a catch all placed early answers every request with 404, including the routes defined after it. I verified this: with the 404 handler first, `GET /orders` returned `{"error":"Route not found"}` and HTTP 404. The fix is to register the catch all last, after all routes.

3. Comparing `req.params.id` without converting it. Route parameters are always strings, so `orders.find((o) => o.id === req.params.id)` never matches a numeric id, and `GET /orders/1` returns 404 even though order 1 exists. I verified this too. The fix is `Number(req.params.id)`, or store ids as strings, but pick one type and use it everywhere.

Where this leaves you

Express earns its place by doing a small job well: routing, middleware, and sensible responses, with nothing forced on top. Start with the order API shape above, keep your routes thin, and let middleware carry the repeated work. The next step is to take an API you already run with plain Node, or one you have only sketched, and rebuild its routes in Express, then check each one with curl the way this post did. If the routes get shorter and the error cases get clearer, you have found the right tool. If the project starts asking for conventions Express will not provide, that is your signal to look at a more opinionated framework instead.

Express and live updates go together: the Socket.IO Tutorial: Real-Time Order Updates in Node.js attaches a Socket.IO server to the same HTTP server and pushes order updates to the browser the moment they change.

Further reading

Frequently asked questions

Is Express still a good choice in 2026?

Yes, for small APIs and services. Express is stable, well documented, and has a large set of middleware, so a small team can ship a JSON API quickly. It is not the best pick for a large app with many features, where a framework with more built-in structure saves time.

Do I need to learn Node.js before Express?

Yes, learn Node.js basics first. Express is a thin layer over Node's HTTP server, so routes, middleware, and error handling all use plain JavaScript and Node patterns. The post shows how a route is just a function that reads a request and writes a response.

What is middleware in Express?

Middleware is a function that runs for a request before the route handler, or between handlers. It can log a request, check authentication, parse a JSON body, or pass an error along. This post uses it to parse JSON bodies with express.json() and to answer unknown routes with a 404, and the same pattern carries logging and authentication.

Why does my Express route return an error for a missing user?

Because the handler looks up an id that does not exist, or throws, and no code turns that into a proper response. Return a 404 with a clear JSON message when the record is missing, as the post's GET /orders/:id route does. Add a catch-all handler at the end of the app for unknown routes, and check that express.json() is registered before any route that reads a body.

Keep reading on this blog

Comments

Popular posts from this blog

Swift Explained: Apple's Language for iPhone, Mac, and More

LangChain Tutorial: Build Your First LLM App in Python

Ollama Tutorial: Run Your First Local LLM in Python