Routing helps your web app decide what to do when someone visits different web addresses. It connects the address they type to the right response or page.
Why routing matters in Express
app.METHOD(PATH, HANDLER)
// METHOD: HTTP method like get, post, put, delete
// PATH: URL path string or pattern
// HANDLER: function to run when route matchesUse app.get() for handling GET requests (like loading a page).
The HANDLER function receives request and response objects to work with.
app.get('/', (req, res) => { res.send('Home page') })
app.post('/submit', (req, res) => { res.send('Form submitted') })
app.get('/users/:id', (req, res) => { res.send(`User ID: ${req.params.id}`) })
This simple Express app uses routing to send different messages for the homepage, about page, and a personalized hello page based on the URL.
import express from 'express' const app = express() const port = 3000 app.get('/', (req, res) => { res.send('Welcome to the homepage!') }) app.get('/about', (req, res) => { res.send('About us page') }) app.get('/hello/:name', (req, res) => { res.send(`Hello, ${req.params.name}!`) }) app.listen(port, () => { console.log(`Server running at http://localhost:${port}`) })
Routing keeps your app organized by separating what happens at each URL.
Order of routes matters: Express checks routes top to bottom and uses the first match.
Use parameters (like :name) to capture parts of the URL dynamically.
Routing connects URLs to code that sends responses.
It helps build websites and APIs that respond differently based on the address.
Express makes routing easy with simple methods like app.get() and app.post().
Practice
Solution
Step 1: Understand routing purpose
Routing links URLs to the code that handles requests and sends responses.Step 2: Eliminate unrelated options
Styling, data storage, and compilation are not routing tasks.Final Answer:
It connects different URLs to specific code that sends responses. -> Option AQuick Check:
Routing = URL to response code [OK]
- Confusing routing with styling or database tasks
- Thinking routing compiles code
- Believing routing stores data
/home in Express?Solution
Step 1: Recall Express GET route syntax
The correct syntax is app.get(path, callback) where callback has (req, res).Step 2: Check each option
app.get('/home', (req, res) => { res.send('Welcome'); }); matches correct syntax; others have syntax errors or misuse.Final Answer:
app.get('/home', (req, res) => { res.send('Welcome'); }); -> Option AQuick Check:
Correct GET route syntax = app.get('/home', (req, res) => { res.send('Welcome'); }); [OK]
- Missing arrow function or callback syntax
- Assigning instead of calling methods
- Passing response directly without function
/about given this Express code?app.get('/about', (req, res) => {
res.send('About us page');
});
app.get('/contact', (req, res) => {
res.send('Contact page');
});Solution
Step 1: Identify the route for '/about'
The code defines app.get('/about') to send 'About us page'.Step 2: Match URL to route
Visiting '/about' triggers the first route, sending 'About us page'.Final Answer:
About us page -> Option DQuick Check:
URL '/about' matches route sending 'About us page' [OK]
- Confusing '/about' with '/contact' route
- Expecting 404 when route exists
- Assuming server crashes without reason
app.get('/hello', (req, res) => {
res.send('Hello World');
});
app.get('/hello') {
res.send('Hi again');
}Solution
Step 1: Check syntax of second route
The second app.get('/hello') lacks a callback function and uses braces incorrectly.Step 2: Verify other options
First route is correct; duplicate routes are allowed; res.send() is valid inside app.get().Final Answer:
Second route has incorrect syntax; missing callback function. -> Option CQuick Check:
Syntax error in second route = Second route has incorrect syntax; missing callback function. [OK]
- Thinking duplicate routes are invalid
- Believing res.send() is disallowed
- Ignoring missing arrow function or callback
'Hello, user!' when visiting /user/:name, where :name is any username. Which code correctly implements this dynamic routing?Solution
Step 1: Understand dynamic route parameter usage
Express uses :name to capture dynamic parts in req.params.name.Step 2: Check each option
app.get('/user/:name', (req, res) => { res.send(`Hello, ${req.params.name}!`); }); uses req.params.name correctly to personalize the message. The static '/user/name' option sends fixed text, the dynamic path with fixed 'Hello, user!' ignores the parameter, and req.query.name is for query strings (?name=foo), not path parameters (/user/foo).Final Answer:
app.get('/user/:name', (req, res) => { res.send(`Hello, ${req.params.name}!`); }); -> Option BQuick Check:
Dynamic param in path = req.params.name [OK]
- Using static path instead of dynamic parameter
- Accessing req.query instead of req.params
- Ignoring dynamic parameter in response
Ready to prove it?
Pass the test and this pattern moves unknown
