devnotes.

HTTP request and response cycle

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:

PropertyDescription
req.appReference to the Express app instance.
req.bodyData submitted by the client (needs a body-parsing middleware).
req.cookiesCookies sent by the client (needs cookie-parser).
req.ipThe client's IP address.
req.pathThe path portion of the URL.
req.routeThe route currently matched.
req.paramsNamed route parameters.
req.queryQuery-string parameters.
req.filesUploaded 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:

MethodDescription
res.appReference 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

  1. A client sends a request — say, a mobile app requesting /products/42.
  2. 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"));
  1. Any middleware registered before the route runs first.
app.use((req, res, next) => {
    console.log("Incoming request logged");
    next();
});
  1. The matched route handler runs and can read req, then build a response with res.
app.get("/order/:id", (req, res) => {
    res.send(`Order #${req.params.id} details`);
});
  1. Express sends the response back to the client.
app.get("/status", (req, res) => {
    res.status(200).json({ message: "OK" });
});
  1. The cycle ends. If the client sends another request, the whole flow repeats.

Status codes at a glance

CodeMeaningExample
200 OKSuccessres.status(200).send('OK')
201 CreatedResource createdres.status(201).json({ message: 'Created' })
204 No ContentSuccess, empty bodyres.status(204).send()
400 Bad RequestClient errorres.status(400).send('Bad Request')
401 UnauthorizedNeeds authenticationres.status(401).json({ error: 'Unauthorized' })
403 ForbiddenAccess deniedres.status(403).send('Forbidden')
404 Not FoundResource missingres.status(404).send('Not Found')
500 Internal Server ErrorServer-side failureres.status(500).send('Server Error')

Choosing the right status code makes APIs easier to consume, debug, and monitor.

HTTP request and response cycle | DevNotes