Express Route Handlers
Explore how to create and use Express route handlers to manage various HTTP requests in a Node.js server environment. Understand how routes define endpoints and handle logic for different actions like retrieving, updating, and deleting data. This lesson builds your skills to structure back-end routes essential for full-stack web applications.
We'll cover the following...
In the previous lesson, we wrote the minimum code necessary to run the server and then accessed it through the browser.
Routes
Every application is usually divided into a bunch of routes. Any page or request we invoke on a website is hitting a specific route on the back-end server. Routes determine how each endpoint should be treated and what logic gets executed.
Each HTTP request type is mapped to a routing method in Express with the syntax app.method(path, callback).
Route methods
The path defines the endpoint or the route path, for example, the / root endpoint. After defining the endpoint, we then perform the matched callback function. Here’s a list of the most common route methods:
- The
getrequest handler is used to retrieve resources from the server. - The
putrequest handleris used for creating and updating the data in the server. - The
postrequest handler is used to create and update the data in the server. The basic difference betweenputandpostmethods is thatpostis used to