Files
freeCodeCamp/curriculum/challenges/english/blocks/lecture-understanding-routing-in-express-js/69a918332f325a9cb8ffad18.md
T
2026-04-24 18:25:01 +00:00

3.2 KiB
Raw Blame History

id, title, challengeType, dashedName
id title challengeType dashedName
69a918332f325a9cb8ffad18 What Are Route Handlers? 19 what-are-route-handlers

--description--

In Express, route handlers (also called route callback functions or controller functions) are functions that define what happens when a client sends a request to a specific route.

A route handler processes the incoming request, performs any necessary logic like querying a database or running calculations, and then sends a response back to the client.

Lets break this down with a simple example (which youve actually been using all along):

app.get("/menu", (req, res) => {
  res.send("Welcome to the menu!");
});

The .get() method is one of Expresss route methods. It listens for GET requests at a specified route. In this example, it listens for requests to /menu.

The function (req, res) => { ... } is the route handler. It runs whenever a client sends a GET request to /menu.

Inside the handler:

  • req (request) is an object that contains information about the incoming request, such as route parameters, query strings, headers, and body data.

  • res (response) is an object used to send a response back to the client.

In this case, res.send("Welcome to the menu!") sends a plain text response to the client.

When a user navigates to /menu in the browser—or sends a GET request to that route—the server executes this handler and responds with the message Welcome to the menu!.

Route handlers are the core of an Express application. They allow you to define how different routes of your web application will respond to requests. You can also add logic to these handlers, such as processing form data, interacting with databases, or performing validation.

--questions--

--text--

What is a route handler in Express?

--answers--

A function that handles incoming requests and sends a response.


A method to handle GET requests.

--feedback--

Consider the purpose of the function in the route.


A way to define a route URL.

--feedback--

Consider the purpose of the function in the route.


A type of middleware.

--feedback--

Consider the purpose of the function in the route.

--video-solution--

1

--text--

In the code app.get('/menu', (req, res) => { res.send('Welcome to the menu!') }), what does the res.send() method do?

--answers--

It defines the route for the /menu.

--feedback--

Think about what happens when the client receives a response.


It sends a response back to the client.


It processes the incoming request.

--feedback--

Think about what happens when the client receives a response.


It performs a database query.

--feedback--

Think about what happens when the client receives a response.

--video-solution--

2

--text--

What type of request does the .get() method in Express listen for?

--answers--

POST requests

--feedback--

Consider what HTTP method is being used in this example.


PUT requests

--feedback--

Consider what HTTP method is being used in this example.


GET requests


DELETE requests

--feedback--

Consider what HTTP method is being used in this example.

--video-solution--

3