How to use Response Object in Express Nodejs

In this article, I will cover the most important information about the response object, its methods, and how to use them in Express.

I. What is a response object

The Response object in Express, often abbreviated as res, gives us a simple way to respond to HTTP requests.

II. Response methods

In Express, there are some response methods:

  • res.send(): Send data
  • res.json(): Return a JSON data
  • res.status(): Specify HTTP response code
  • res.redirect(): Redirect to a certain path
  • res.render(): Return a view template

1. res.send()

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
});

2. res.json()

res.json() will return JSON.

app.get("/", (req, res) => {
  res.json({ people: ["Anna", "John"] }); // Send JSON data.
});

3. res.status()

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.

  • 1xx : Information
  • 2xx: Success
  • 3xx: Redirects
  • 4xx: Error in clients
  • 5xx: Server error

4. res.redirect()

You can redirect customers to routes in your app or to different websites:

app.get("/nana", (req, res) => {
  res.redirect("/home");
});

5. res.render()

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.

III. Example response methods

1. Send simple HTML text

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>");
});

2. Send HTML Pages

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 });
});

3. Redirects

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");
});

4. 404 page

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);