📖 Scenario: You are building a simple web server that responds differently based on the URL path the user visits. This is like a restaurant where different doors lead to different rooms. Routing helps the server know which response to send for each door (URL).
🎯 Goal: Create an Express server with routes for the homepage, about page, and contact page. Each route should send a unique message to the browser.
📋 What You'll Learn
Create an Express app instance
Set up a route for '/' that sends 'Welcome to the homepage!'
Set up a route for '/about' that sends 'Learn more about us.'
Set up a route for '/contact' that sends 'Contact us here.'
Start the server listening on port 3000
💡 Why This Matters
🌍 Real World
Routing is how web servers decide what content to show when users visit different URLs. This is essential for websites and APIs.
💼 Career
Understanding routing is a key skill for backend developers working with Express or similar web frameworks.
Progress0 / 4 steps
1
Set up Express app
Create a variable called express that requires the 'express' module. Then create a variable called app by calling express().
Express
Hint
Use require('express') to import Express and then call it as a function to create the app.
2
Add homepage route
Use app.get with the path '/' and a callback function with parameters req and res. Inside the callback, send the text 'Welcome to the homepage!' using res.send().
Express
Hint
Use app.get to define a route for the homepage path '/' and send a welcome message.
3
Add about and contact routes
Add two more routes using app.get: one for '/about' that sends 'Learn more about us.', and one for '/contact' that sends 'Contact us here.'. Use req and res as parameters in the callback functions.
Express
Hint
Define two more routes with app.get for '/about' and '/contact' paths, sending the specified messages.
4
Start the server
Use app.listen to start the server on port 3000. Add a callback function that takes no parameters and does nothing inside.
Express
Hint
Use app.listen with port 3000 and an empty callback function to start the server.
Practice
(1/5)
1. Why is routing important in an Express application?
easy
A. It connects different URLs to specific code that sends responses.
B. It styles the webpage with CSS automatically.
C. It stores data permanently in a database.
D. It compiles JavaScript into machine code.
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 A
Quick Check:
Routing = URL to response code [OK]
Hint: Routing matches URLs to code that sends responses [OK]
Common Mistakes:
Confusing routing with styling or database tasks
Thinking routing compiles code
Believing routing stores data
2. Which of the following is the correct way to define a GET route for the path /home in Express?
easy
A. app.get('/home', (req, res) => { res.send('Welcome'); });
B. app.route('/home').get = (req, res) => { res.send('Welcome'); };
C. app.get('/home') { res.send('Welcome'); }
D. app.get('/home', res.send('Welcome'));
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 A
C. Second route has incorrect syntax; missing callback function.
D. res.send() cannot be used inside app.get().
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 C
Quick Check:
Syntax error in second route = Second route has incorrect syntax; missing callback function. [OK]
Hint: Check for callback function syntax in routes [OK]
Common Mistakes:
Thinking duplicate routes are invalid
Believing res.send() is disallowed
Ignoring missing arrow function or callback
5. You want to create an Express app that responds with 'Hello, user!' when visiting /user/:name, where :name is any username. Which code correctly implements this dynamic routing?
hard
A. app.get('/user/name', (req, res) => { res.send('Hello, name!'); });
B. app.get('/user/:name', (req, res) => { res.send(`Hello, ${req.params.name}!`); });
C. app.get('/user/:name', (req, res) => { res.send('Hello, user!'); });
D. app.get('/user/:name', (req, res) => { res.send(req.query.name); });
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).