In this article, I will cover the most important information about the response object, its methods, and how to use them in Express.
The Response object in Express, often abbreviated as res, gives us a simple way to respond to HTTP requests.
In Express, there are some response methods:
res.send() is probably the most famous method used on res. With res.send() you can respond to HTTP requests with all kinds of data.
app.get("/", (req, res) => {
res.send({ people: ["Anna", "John"] }); // Send JSON data.
res.send("<h1>Hello World</h1>"); // Send HTML
res.send("Normal text"); // Send text
});
res.json() will return JSON.
app.get("/", (req, res) => {
res.json({ people: ["Anna", "John"] }); // Send JSON data.
});
res.status specifies the HTTP response code, representing the response’s status.
app.get('/', (req, res) => {
res.status(302)
});
Each code will represent a different response state.
You can redirect customers to routes in your app or to different websites:
app.get("/nana", (req, res) => {
res.redirect("/home");
});
If you combine Express with template engines like Pug, EJS, then this method will automatically compile these templates to normal HTML and send feedback to clients.
In Express, to send simple HTML text, we can use .send()
method:
const express = require("express");
// express app
const app = express();
// listen for requests
app.listen(3000);
app.get("/", (req, res) => {
res.send("<p>home page</p>");
});
app.get("/about", (req, res) => {
res.send("<p>about page</p>");
});
We use the sendFile()
to provide routing in Express. By default, it’ll look for an absolute path from the root of our computer.
If we provide a relative path, we’ll need to tell where it’s relative from.
To do that, we pass in { root: __dirname }
object as a second argument to specify what the root should be.
const express = require("express");
// express app
const app = express();
// listen for requests
app.listen(3000);
app.get("/", (req, res) => {
res.sendFile("./views/index.html", { root: __dirname });
});
app.get("/about", (req, res) => {
res.sendFile("./views/about.html", { root: __dirname });
});
To redirect pages in Express, we can use the redirect()
method. Also, it automatically sets the status code.
For example, we redirect the “about-us” page to the “about” page:
// redirects
app.get("/about-us", (req, res) => {
res.redirect("/about");
});
To set a 404 page in Express, we use the use()
method. We use this method to create Middleware and fire Middleware functions in Express.
In this case, we pass in a callback function that sets a status code to 404 and sends a 404.html file to the browser.
// 404 page
app.use((req, res) => {
res.status(404).sendFile("./views/404.html", { root: __dirname });
});
Because Express doesn’t realize we’re sending a 404 page, we must include the status()
method manually.
Note
When we type an address in the browser, Express will check every route. If none matches, it will return a 404 page.
Therefore, the position of the 404 route must be placed at the end of the routes.
const express = require("express");
// express app
const app = express();
app.get("/", (req, res) => {
res.sendFile("./views/index.html", { root: __dirname });
});
app.get("/about", (req, res) => {
res.sendFile("./views/about.html", { root: __dirname });
});
// redirects
app.get("/about-us", (req, res) => {
res.redirect("/about");
});
// 404 page
app.use((req, res) => {
res.status(404).sendFile("./views/404.html", { root: __dirname });
});
// listen for requests
app.listen(3000);