res.end() finishes the response process. It comes straight from Node's core
http.ServerResponse, and can be used with or without data.
Syntax
res.end([data[, encoding]][, callback])
Example
const express = require("express");
const app = express();
const PORT = 4800;
app.use("/ping", (req, res, next) => {
console.log("ping middleware hit");
res.end("pong");
});
app.listen(PORT, () => console.log(`Listening on ${PORT}`));
Use cases
- Ending a response without sending a body.
- Finishing a response after writing data manually with lower-level methods.
- Explicitly terminating the response inside middleware.