Express Routing

Level : Intermediate
Mentor: Shailendra Chauhan
Duration : 00:04:00

What is routing?

Routing is used to handle application URIs. Routing enables us to react to client requests. When you send a request, the express route middleware processes it. If a route is found, the request is processed further; otherwise, it returns a 404 error.

What is Express.js Routing?

Define endpoints that handle various HTTP requests (GET, POST, PUT, DELETE, and so on), as well as URL patterns for incoming requests. To design RESTful APIs or web application endpoints, divide application logic into modular components based on routes, with callbacks or middleware executed for each route to quickly access request parameters, query strings, and bodies.

Define Routes in Express.js

Express offers an effective method for managing and handling routes in a web application. Express includes methods for dealing with HTTP verbs such as get(), post(), put(), and delete(). All of these methods use the first parameter to indicate the URL and a sequence of middleware as arguments.

Router Class in Express.js

Express.js' Router class provides a middleware-like framework for routing within Node.js applications, allowing route-handling logic to be modularized and organized. It lets you define routes, specify HTTP methods, and run callbacks or middleware.

Uses of Router Class

  • Used to design a modular route handler.​
  • Create middleware and HTTP method routes.​
  • Separate the routes into their file.

Router Class Methods

  • router.route(): Create a unique route.​
  • router.METHOD(): Provide routing capabilities, where METHOD is the HTTP method (e.g., GET, PUT, POST, DELETE) in lowercase.​
  • router.all(): Like router.METHOD(), but matches all HTTP verbs. Useful for implementing "global" logic for certain path prefixes or arbitrary matches. For example, user authentication and authorization.​
  • router.use(): Used to specify middleware function with an optional path, which defaults to "/".

 

Router Object

The Router object in Express.js represents an instance of the Express router, which allows you to arrange route handlers and middleware into modular, reusable parts. It enables you to define routes for a certain section of your application independently and then utilize them as middleware in your main application.

Route Methods

Express.js handles requests using route methods, which include HTTP methods like GET, POST, PUT, and DELETE. Express includes methods for each HTTP request type, such as get(), post(), put(), and delete(). These methods specify how your application reacts to client requests for a specific endpoint.

GET() Method

The GET() works is essentially a request for data or resources that is served by the server with a status code of 200, which means "OK". If the request is invalid, it will return the status code 404. Here is a list of the starting numbers and their meanings for status codes.

POST() Method

The POST() method is used to deliver data from the client to the server, which then responds with a status code of 200. If the request is invalid, the status code 404 is returned, just as the GET() method.

PUT() Method

The PUT() method is used to update the existing data of a single object on the server, and it returns a status code of 200. If the request is invalid, a 404 status code is returned, similar to the GET() and POST() methods.

DELETE() Method

The delete() method is used to erase data from the server; it also returns a status code of 200. If the request is invalid, the status code 404 is returned, just like any other HTTP method.

Route Paths

In Express.js, route paths determine the URL pattern that will trigger a specific route. They can be simple strings or patterns that contain route parameters.

Route parameters

Route parameters are dynamic components of the route path that contain values given by the client. They start with a colon (:) in the route path definition. Express retrieves the arguments from the URL and stores them in the req.params object.

Router Route

Express.js router routes are modular units that group similar route handlers and middleware. It is an instance of the Express router. Route handlers and middleware are defined on the router object, just like they are on the main Express application.

Route Handlers

Express.js route handlers are functions that are called when routes match. They produce responses to client requests by passing two parameters: req for the request object and res for the response object. Handlers can perform database actions, render views, and send JSON responses. They might be synchronous or asynchronous, depending on their tasks.

Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this