Routing refers to determining how an application responds to a client request to a particular endpoint, which is a URI (or path) and a specific HTTP request method (GET, POST, etc.). Each route can have one or more handler functions, which are executed when the route is matched.
Express routing enables us to:
Express provides methods that correspond to HTTP methods. The most common ones are:
server.get()
- Handle GET requests (retrieve data)server.post()
- Handle POST requests (create new data)server.put()
- Handle PUT requests (update existing data)server.delete()
- Handle DELETE requests (remove data)Each method takes two arguments: the route path and the callback function(s) that handle the request and response.
Let's look at a basic example of routing in action. First, to make our Express application respond to GET
requests on different URLs, add the following endpoints:
// this request handler executes when making a GET request to /about
server.get('/about', (req, res) => {
res.status(200).send('<h1>About Us</h1>');
});
// this request handler executes when making a GET request to /contact
server.get('/contact', (req, res) => {
res.status(200).send('<h1>Contact Form</h1>');
});
Two things to note:
Open a browser and navigate to the /about
and /contact
routes. The appropriate route handler will execute.
When responding to requests, it's important to use the appropriate HTTP status codes:
Let's write endpoints that execute different request handlers on the same URL by changing the HTTP method.
First, add a POST endpoint:
// this request handler executes when making a POST request to /hobbits
server.post('/hobbits', (req, res) => {
res.status(201).json({ url: '/hobbits', operation: 'POST' });
});
Next, add a PUT endpoint:
// this request handler executes when making a PUT request to /hobbits
server.put('/hobbits', (req, res) => {
res.status(200).json({ url: '/hobbits', operation: 'PUT' });
});
Finally, add a DELETE endpoint:
// this request handler executes when making a DELETE request to /hobbits
server.delete('/hobbits', (req, res) => {
res.status(204);
});
Note the different HTTP status codes we're using:
You may have noticed that we are not reading any data from the request, as that is something we'll learn later in the module. We are about to learn how to use a tool called Postman
to test our POST
, PUT
, and DELETE
endpoints.
While you can test GET requests easily in a browser, other HTTP methods (POST, PUT, DELETE) require specialized tools. Some popular options include:
These tools allow you to craft custom HTTP requests with specific headers, body content, and parameters.
Add endpoints to handle POST
, PUT
, and DELETE
for the /users
route. Return a JSON response similar to what we did in the code-along.
Try implementing the following functionality:
Test your endpoints using Postman or another API testing tool.
Server-Side Routing with Express
Server-Side Routing with Express Solution
The versions of project dependencies used in the recording are slightly different from the ones used in the starter and solution repositories, but this should not affect the relevant code of the Guided Project.
The versions used in the repositories are more recent, and thus more similar to the versions you will install if you create a project from scratch.