image
Introduction to Express - Request Object Methods

Contents

Introduction to Express - Request Object Methods

The course is part of this learning path

Introduction to Express - Request Object Methods
Difficulty
Beginner
Duration
5m
Students
5
Ratings
5/5
starstarstarstarstar
Description

In this course, you will learn what the Express Request Object is and implement some commonly used response methods.

Learning Objectives

  • Explain the request object
  • Implement req.body
  • Learn what middleware is
  • Implement the middleware express.json() to parse req.body
  • Implement req.method
  • Implement req.path

Intended Audience

This course is intended for anyone who wants to learn about Express.

Prerequisites



Transcript

Introduction to Express- Request Object Methods. The Request object is one of the special object arguments that is typically given as part of an Express route. The request object represents the HTTP requests sent to the Express application from the client. In this video, I'm going to cover some commonly used request object methods. Currently, on the screen, I have an existing API route. To begin inside of the route, I'm going to console.log(req.body); The body portion of the request object is an object itself that contains the pairs of data from the client side requests, and this request comes in the JSON format. This leads to an interesting situation. By default, the Express server is not set up to parse JSON data. I need to use an Express middleware to handle this situation. What is middleware?

Middleware is any function that executes between the response and request cycle, as in the middle of the cycle. So, how do I implement this middleware? Up above, I will type app.use(), I will insert express.Json. This built-in middleware that's part of Express parses incoming requests with JSON payloads. To demonstrate how this works, on the right half of the screen, I have Insomnia open and I will use this to send a JSON GET request. In the editor, I will create a JSON object with a property of FirstName that has a value of Farish for that FirstName. I will hit 'Send' and in the terminal, there is a console.log that shows an object with FirstName as the property and Farish as its value. 

And this allows for access to the properties of the body object, allowing you to work with this data in a manner that fits your use case. Next, I'm going to discuss req.method. Req.method holds the value of the request method. Inside of the route, I will change body to method and hit 'Send' again inside of Insomnia. And in the terminal, GET appears, representing the method used for this request. Now for demonstration purposes inside of the IDE, I'm going to copy and paste this route, and in the copy I'm going to replace the GET method with a POST method. Inside of Insomnia, I will change the GET method to a POST method and hit 'Send'. Now in the terminal is a console.log.post. You may be wondering what is the use case for req.method since the routes have predefined request methods? 

This can be used for custom middleware that its behavior is dependent on the method of the HTTP request. The last method I will cover is req.path. The req.path method shows the path of the URL requests. I'm going to start out by commenting out the post route below. Now in the GET route inside of the console.log, I'm going change method to path. For demonstration purposes, I will also add a wildcard endpoint which is created with the /* after API. To the right, I will change the Insomnia request URL by adding /users to the end of the URL, and I will hit 'Send'. In the terminal, we can see the console.log of /api/users. So, I've captured the path of the request. Because I'm using a wildcard, I can also capture other paths sent to this URL. I will replace users with customers in Insomnia and hit 'Send' again. And now in the terminal, the console.log shows /api/customers. So, what is the use case for path capturing? In theory, you can create dynamic routes based on the path given, or you can use this for air hailing. And that's it, thanks for watching at Cloud Academy.

 

About the Author
Students
5202
Labs
24
Courses
64
Learning Paths
31

Farish has worked in the EdTech industry for over six years. He is passionate about teaching valuable coding skills to help individuals and enterprises succeed.

Previously, Farish worked at 2U Inc in two concurrent roles. Farish worked as an adjunct instructor for 2U’s full-stack boot camps at UCLA and UCR. Farish also worked as a curriculum engineer for multiple full-stack boot camp programs. As a curriculum engineer, Farish’s role was to create activities, projects, and lesson plans taught in the boot camps used by over 50 University partners. Along with these duties, Farish also created nearly 80 videos for the full-stack blended online program.

Before 2U, Farish worked at Codecademy for over four years, both as a content creator and part of the curriculum experience team.

Farish is an avid powerlifter, sushi lover, and occasional Funko collector.