Different Express Response Methods

Learn via video courses
Topics Covered

Overview

Express.js is a simple framework that works on top of Node.js to simplify the overall coding process of the application. In this article, we will discuss the most significant facts about the response object, its methods, and how to utilize them in Express.

Introduction

The request-response cycle can be ended by using the methods on the response object (res) to send a response to the client. The client request will be abandoned if none of these methods is used by a route handler.

In Express, a few of the response methods are as follows:

  • res.send(): to send data
  • res.status(): Specify HTTP response code
  • res.json(): to return the JSON data We are going to look at the syntax and examples in detail in the later section of the article.

What are Response Methods?

The Response object (response) provides the HTTP response that an Express app sends when it receives an HTTP request.

Let's look at some properties of the response object.

PropertiesDescription
res. appIt keeps track of the express application instance that is utilizing the middleware.
res.headersSentWhether the app supplied HTTP headers with the response is indicated by this Boolean value.
res. localsIt designates an object with response local variables that are focused on the request.

We will discuss the important response methods in the next section of the article.

Different Types of Response Methods

  1. res.send() : Most people are already familiar with the res.send() method. You can answer HTTP requests with a variety of data types using res. send().
    Syntax
    res.send([body])
    Example

  2. res.json(): It is used for sending a JSON response. This function uses JSON.stringify() to transform the parameter into a JSON string and sends a response (with the correct content type).
    Syntax
    res.json([body])
    Example

  3. res.status(): Represents the status of the response, res. status defines the HTTP response code. If an unidentified status code is provided, the response body will just contain the code's numeric value.

    Each code will represent a different response state.

    • 5xx: Server error
    • 4xx: Error in clients
    • 3xx: Redirects
    • 2xx: Success
    • 1xx : Information
      Syntax
      res.status(statusCode)
      Example
  4. res.redirect(): redirects to a URL derived from the given path with the specified status, which is a positive number that matches an HTTP status code. Status defaults to "302 "Found" if it is not given.
    Syntax
    res.redirect([status,] path)

    Example

  5. res.render(): If you use Express in conjunction with a template engine like Pug or EJS, this technique will automatically compile the templates to standard HTML and deliver client feedback.
    Syntax
    res.render(view [, locals] [, callback]

    • locals: an object called locals, whose properties specify local variables for the view.
    • a callback function: The method does not execute an automatic response but instead returns the rendered string and any potential errors if they are provided. When an error occurs, the method internally calls next(err).
    • View: The string file path of the view file to be rendered is the view argument.

    Example

  6. res.download(): The file at the path is transferred as an "attachment." Browsers will typically prompt the user to download. The path argument is used by default to derive the Content-Disposition header "filename=" parameter, although this can be modified with the filename parameter. If the path is relative, it will be based on the process's current working directory or the root option, if specified.
    Syntax
    res.download(path [, filename] [, options] [, fn])

    Example

  7. res.end(): The response procedure will end using this method. This method was inspired by Node core, notably the response. end() method of HTTP.ServerResponse. Use to swiftly terminate the response with no data. If you want to send the response with data, instead use res. send() and res.json().
    Syntax
    res.end([data] [, encoding])
    Example

  8. res.send file (): Transfers the file at the specified path. Sets the Content-Type response HTTP header field based on the extension of the filename. The path should be a complete path to the file unless the root option is provided in the options object.
    Syntax
    res.send file(path [, options] [, fn])
    Example

Conclusion

  • Express.js is a simple framework that works on top of Node.js to simplify the overall coding process.
  • The Response object (response) provides the HTTP response that an Express app sends when it receives an HTTP request.
  • Different types of response methods are res.send(), res.sendFile(), res.end(), res.download(), and res.render() etc.