Module 2 - Server-Side Routing with Express

Objectives

Understanding Express Routing

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:

  • Organize our application based on URL paths and HTTP methods
  • Execute specific code based on the client's request
  • Separate concerns by breaking the application into smaller, more manageable parts
  • Create a modular API with clear, predictable endpoints

Route Methods in Express

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.

Basic Routing Example

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:

  • We use the same HTTP Method on both endpoints, but Express looks at the URL and executes the corresponding request handler.
  • We can return a string with valid HTML!

Open a browser and navigate to the /about and /contact routes. The appropriate route handler will execute.

HTTP Status Codes

When responding to requests, it's important to use the appropriate HTTP status codes:

  • 200 (OK) - Standard response for successful HTTP requests, typically used for GET and PUT
  • 201 (Created) - The request has been fulfilled and resulted in a new resource being created, typically used for POST
  • 204 (No Content) - The server successfully processed the request, but is not returning any content, typically used for DELETE
  • 400 (Bad Request) - The server cannot process the request due to a client error
  • 404 (Not Found) - The requested resource could not be found
  • 500 (Internal Server Error) - A generic error message when the server encounters an unexpected condition

Testing API Endpoints

While you can test GET requests easily in a browser, other HTTP methods (POST, PUT, DELETE) require specialized tools. Some popular options include:

  • Postman - A powerful GUI tool for API testing
  • Insomnia - Another GUI-based REST client
  • curl - Command-line tool for transferring data with URLs
  • HTTPie - Human-friendly command-line HTTP client

These tools allow you to craft custom HTTP requests with specific headers, body content, and parameters.

Challenge

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:

  • POST to /users - Return a 201 status code and a JSON object with information about the operation
  • PUT to /users - Return a 200 status code and relevant information about the update
  • DELETE to /users - Return a 204 status code to indicate successful deletion with no content

Test your endpoints using Postman or another API testing tool.

Guided Project

Project Resources

Important Notes

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.

Assignment

Project Resources