1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Introduction to Express - Route Methods: All and Route

Introduction to Express - Route Methods: All and Route

Contents

keyboard_tab
Introduction to Express - Route Methods: All and Route
Introduction to Express - Route Methods: All and Route
Difficulty
Intermediate
Duration
4m
Students
5
Ratings
5/5
starstarstarstarstar
Description

In this course, you will learn about two route methods in Express that will help you create more efficient and modular routes. 

Learning Objectives

  • Understand how the app.all() route handler works to handle all HTTP request methods
  • Implement app.all()
  • Understand how the app.route() method works to create chainable route methods
  • Implement app.route()

Intended Audience

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

Prerequisites

Transcript

Introduction to Express route methods all and route. In this video, I'm going to discuss two specific Express route methods that may help simplify your code. To begin on the screen, I have three existing routes. A GET route, a POST route and a PUT route. Sometimes in the real world we have API routes that serve a specific purpose and regardless of the request method that is used, the response will be the same. On the right using insomnia, I'm going to send a GET request, then a POST request and a PUT request to demonstrate that these routes return the same response. If in your API design you expect your application to give the same response for a specific route for all request methods, writing multiple routes to handle this scenario is pretty inefficient. In Express, there is a .all method to cover all rest method requests sent to a specific route path. 

To demonstrate this, I'm going to comment out the POST and PUT routes. For the .get method, I'm going to change that to .all. What I'm stating with this route change is that any request method that is sent to the specific route path will be managed by this route handler. To demonstrate this on the right using Insomnia, I will change the current method to DELETE which is a route method that did not previously exist or currently exist in the Express application. I will click on 'Send' and there is a response. Demonstrating that the .all method can be used when sending back the same response for all method requests for a specific route path. Now to discuss a different scenario, transitioning back to the initial structure of this Express application that was shown at the beginning of this video. I have three routes each for the same path, but for a different HTTP method. Writing the same path multiple times is not that efficient. 

Instead of writing each route separately, the .route method allows for the writing of changeable route method requests. Transitioning back to the lab environment, I will begin by deleting the comments and the arrow function that's inside of the app.all route. Now I'm going to change .all to .route. Looking at this new structure, there is now app.route along with the path for this route. From here, I can chain route methods. On the next line, I will type .get passing in the requests response arrow function. Inside of the arrow function, I will paste in the response that was originally in the .all method. Now I can continue the chain. 

On the next line, I will type .post passing in the request response arrow function. Inside of the arrow function, I will type res.json passing in a JSON object with the property of POST and a template string of the user ID from the request parameters object. What I've done is create one path with two request methods. Removing repetitive route building when one path will suffice. Now using Insomnia, I will send a request for the GET method and there is a response. I will change GET to POST and hit 'Send' again, and there is the POST response with the user ID. To recap, the focus of this video was to express methods to help reduce the repetitiveness of your code. And that's it. Thanks for watching at Cloud Academy.

 

About the Author
Students
4009
Labs
24
Courses
61
Learning Paths
24

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.