Every interaction between a client and an Express server follows the same pattern: a request
comes in, gets processed, and a response goes out. Express represents these two sides with
the req and res objects.
The req object
req carries everything about the incoming request — URL, params, query string, body,
headers, cookies.
app.get("/", (req, res) => {
// read data from req here
});
Frequently used req properties:
| Property | Description |
|---|---|
req.app | Reference to the Express app instance. |
req.body | Data submitted by the client (needs a body-parsing middleware). |
req.cookies | Cookies sent by the client (needs cookie-parser). |
req.ip | The client's IP address. |
req.path | The path portion of the URL. |
req.route | The route currently matched. |
req.params | Named route parameters. |
req.query | Query-string parameters. |
req.files | Uploaded files (needs an upload middleware). |
req.is() | Checks the request's content type against a given MIME type. |
The res object
res is how you send data back — HTML, JSON, files, status codes.
Frequently used res methods:
| Method | Description |
|---|---|
res.app | Reference to the app instance. |
res.append() | Appends a value to a response header. |
res.cookie() | Sets a cookie. |
res.get() | Reads a response header's current value. |
res.end() | Ends the response. |
res.json() | Sends a JSON response. |
res.links() | Sets Link response headers. |
res.render() | Renders a view template. |
res.location() | Sets the Location header. |
res.send() | Sends a response of any type. |
res.set() | Sets a response header. |
res.status() | Sets the HTTP status code. |
Walking through a request
- A client sends a request — say, a mobile app requesting
/products/42. - Express matches the URL and method to a registered route.
const express = require("express");
const app = express();
app.get("/", (req, res) => {
res.send("Order Service is live");
});
app.listen(4600, () => console.log("Listening on 4600"));
- Any middleware registered before the route runs first.
app.use((req, res, next) => {
console.log("Incoming request logged");
next();
});
- The matched route handler runs and can read
req, then build a response withres.
app.get("/order/:id", (req, res) => {
res.send(`Order #${req.params.id} details`);
});
- Express sends the response back to the client.
app.get("/status", (req, res) => {
res.status(200).json({ message: "OK" });
});
- The cycle ends. If the client sends another request, the whole flow repeats.
Status codes at a glance
| Code | Meaning | Example |
|---|---|---|
| 200 OK | Success | res.status(200).send('OK') |
| 201 Created | Resource created | res.status(201).json({ message: 'Created' }) |
| 204 No Content | Success, empty body | res.status(204).send() |
| 400 Bad Request | Client error | res.status(400).send('Bad Request') |
| 401 Unauthorized | Needs authentication | res.status(401).json({ error: 'Unauthorized' }) |
| 403 Forbidden | Access denied | res.status(403).send('Forbidden') |
| 404 Not Found | Resource missing | res.status(404).send('Not Found') |
| 500 Internal Server Error | Server-side failure | res.status(500).send('Server Error') |
Choosing the right status code makes APIs easier to consume, debug, and monitor.