Co-authored-by: majestic-owl448 <26656284+majestic-owl448@users.noreply.github.com> Co-authored-by: Ilenia M <nethleen@gmail.com>
3.2 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 69a9182c1bc7cc1a76116635 | What Are Route Paths in Express? | 19 | what-are-route-paths-in-express |
--description--
In Express, route paths are the URL patterns that determine how the server responds to incoming requests. When a user requests a specific URL, Express matches that URL to a route path and runs the corresponding handler.
Route paths are defined inside route methods like app.get(), app.post(), and so on. These paths can be static, such as /home, or dynamic, where parameters are used to capture values from the URL, like /user/:id.
In this lesson we’ll focus on static routes. Here’s an example of one:
// Static route
app.get("/home", (req, res) => {
res.send("Welcome to the homepage!");
});
In this example, the server listens for GET requests to /home and responds with a simple message.
The app.get() method takes two arguments:
-
The first argument,
"/home", is the static route path (or URL) that the server listens for. -
The second argument is a callback function that runs when a GET request is made to that path.
Inside the callback, res.send() sends a response back to the client. In this case, it sends the text Welcome to the homepage!, which is displayed in the browser.
Here’s another example of a static route:
// Static route
app.get("/menu/drinks", (req, res) => {
res.send("Welcome to the drinks menu!");
});
In this case, the server listens for GET requests to the /menu/drinks URL and sends back a message when that route is requested.
You might use routes like this when your site has multiple related pages grouped under a common path. For example, a restaurant website could organize its menu pages like this:
-
/menu/food -
/menu/drinks -
/menu/desserts
This approach helps keep your URLs clear and meaningful, and it also makes your routes easier to organize and maintain as your application grows.
In the next lesson, we’ll look at dynamic route paths with parameters.
--questions--
--text--
What does the route app.get("/home", ...) respond to?
--answers--
POST requests to /home
--feedback--
Look at the first parameter.
GET requests to /home
GET requests to /menu/drinks
--feedback--
Look at the first parameter.
POST requests to /menu/drinks
--feedback--
Look at the first parameter.
--video-solution--
2
--text--
What does the route app.get("/menu", ...) respond to?
--answers--
POST requests to /menu
--feedback--
Look at the first parameter.
GET requests to /menu
GET requests to /menu/drinks
--feedback--
Look at the first parameter.
POST requests to /menu/drinks
--feedback--
Look at the first parameter.
--video-solution--
2
--text--
What does the route app.get("/menu/drinks", ...) respond to?
--answers--
POST requests to /menu
--feedback--
Look at the first parameter.
GET requests to /menu
--feedback--
Look at the first parameter.
GET requests to /menu/drinks
POST requests to /menu/drinks
--feedback--
Look at the first parameter.
--video-solution--
3