1. What is Express
Node.js lets you run JavaScript on a server โ but out of the box it gives you very low-level tools. Writing HTTP servers in raw Node requires a lot of boilerplate: parsing URLs, reading request bodies, setting response headers. It gets tedious fast.
Express is a minimal, unopinionated web framework that wraps Node's HTTP module and gives you clean, readable tools for the things you actually need: defining routes, reading request data, and sending responses.
UrbanCart's React app runs in the browser โ it shows product listings, cart state, and checkout forms. But where does the product data come from? A database. Express sits in the middle: it receives requests from the React frontend (or a mobile app, or curl), talks to the database, and sends back JSON. It is the API layer your frontend calls.
fetch('/api/products')
app.get('/api/products')
PostgreSQL / MongoDB
Express is one of the most downloaded npm packages of all time. Almost every Node.js backend you encounter โ from startups to large-scale production systems โ is built on Express or a framework derived from it.
npm install express in your Node.js project. That's it. No global install needed โ Express is a regular npm dependency.
2. Creating a Server
An Express server is created in four lines. You import Express, create an app instance, define at least one route, and start listening on a port.
app = express()
Creates your Express application. This is the central object โ you attach routes, middleware, and configuration to it.
app.listen(3000)
Starts an HTTP server on port 3000. The callback fires once the server is ready to accept connections.
app.listen(process.env.PORT || 3000). Your React dev server typically runs on port 5173 or 3000 โ if there's a conflict, change one.
3. Routes โ GET, POST, PUT, DELETE
A route is a combination of an HTTP method and a URL path. Express gives you a method for each HTTP verb. Each method takes a path and a handler function that receives req (request) and res (response).
Read
Fetch a resource. Safe โ no side effects.
Create
Create a new resource. Sends data in the body.
Update
Replace an existing resource entirely.
Delete
Remove a resource permanently.
4. The req and res Objects
Every route handler receives two objects: req (request) contains everything the client sent, and res (response) contains the tools to send something back.
req โ Reading the request
req.params
URL route parameters. /products/:id โ req.params.id. Always a string.
req.query
Query string values. ?sort=price โ req.query.sort. Great for filtering and sorting.
req.body
JSON body from POST/PUT requests. Requires app.use(express.json()) middleware first.
req.headers
HTTP request headers. Commonly used for auth tokens: req.headers.authorization.
res โ Sending the response
5. Route Parameters โ /api/products/:id
Route parameters let you capture dynamic values from the URL path. You declare them with a colon (:) in the route definition, and access them via req.params.
/api/products/9999 and that ID doesn't exist, return a 404 with a clear error message. Never let the server return undefined or crash silently.
6. Query Strings โ Filtering & Sorting
Query strings appear after the ? in a URL. They are used for optional parameters like filtering, sorting, and pagination. Unlike route parameters, they are not part of the route definition โ Express parses them automatically and puts them in req.query.
:id) for identifying a specific resource โ /products/42. Use query strings (?category=tech) for optional filters, sorting, and pagination. Never mix these up.
7. Returning JSON โ Always Use res.json()
REST APIs communicate in JSON. Express's res.json() serialises any JavaScript object or array, sets the correct Content-Type: application/json header automatically, and sends the response.
8. Complete UrbanCart Products API
Let's put everything together into a complete, working products API. This is the real structure you'll use in production โ the same pattern powers Flipkart, Jumia, and Shopify's backends.
products array with database queries and you have production-ready code. The route structure, status codes, and response shapes are exactly what Flipkart, Jumia, and Shopify use.
Quiz
app.listen(3000) do?/products/:id inside an Express route handler??sort=price in an Express route handler?You completed the Express Basics quiz.
You just built a REST API from scratch.
Now build a backend career that scales.
Join the Best AI Programs โ three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses โ