📖 Scenario: You are building a simple web server that responds to user requests. You want to create a route that listens for GET requests on a specific path and sends back a friendly message.
🎯 Goal: Create an Express server with a GET route at /hello that responds with the text Hello, Express!.
📋 What You'll Learn
Create an Express app instance called app
Set up a GET route on /hello
Send the response Hello, Express! when the route is accessed
Listen on port 3000
💡 Why This Matters
🌍 Real World
Web servers often need to respond to user requests on different paths. GET routes are the most common way to serve pages or data.
💼 Career
Understanding how to handle GET routes in Express is essential for backend web development jobs using Node.js.
Progress0 / 4 steps
1
Set up Express app
Write code to import Express and create an app instance called app.
Express
Hint
Use require('express') to import Express and then call it to create app.
2
Create GET route
Add a GET route on /hello using app.get that takes req and res as parameters.
Express
Hint
Use app.get('/hello', (req, res) => { ... }) to create the route.
3
Send response text
Inside the GET route callback, use res.send to send the text Hello, Express!.
Express
Hint
Use res.send('Hello, Express!') inside the route callback.
4
Start server on port 3000
Add code to make the app listen on port 3000 using app.listen.
Express
Hint
Use app.listen(3000) to start the server on port 3000.
Practice
(1/5)
1. What does the app.get() method do in an Express app?
easy
A. Starts the Express server and listens on a port
B. Defines a route to handle GET requests at a specific URL
C. Sends a POST request to a server
D. Deletes a route from the Express app
Solution
Step 1: Understand the purpose of app.get()
The app.get() method is used to define a route that listens for GET requests on a given URL path.
Step 2: Compare with other methods
Other methods like app.post() handle POST requests, and app.listen() starts the server, so they do not define GET routes.
Final Answer:
Defines a route to handle GET requests at a specific URL -> Option B
Quick Check:
app.get() defines GET routes [OK]
Hint: app.get() always sets up a GET route handler [OK]
Common Mistakes:
Confusing app.get() with app.listen()
Thinking app.get() sends requests instead of handling them
Mixing GET with POST methods
2. Which of the following is the correct syntax to create a GET route for the path /hello that sends "Hello World" as a response?
easy
A. app.get('/hello', (req, res) => { res.send('Hello World'); });
B. app.get('/hello', (res, req) => { res.send('Hello World'); });
C. app.get('/hello', (req, res) => { res.write('Hello World'); });
D. app.get('/hello', (req, res) => { res.sendHello('Hello World'); });
Solution
Step 1: Check parameter order in callback
The callback function parameters must be req first, then res. app.get('/hello', (res, req) => { res.send('Hello World'); }); reverses them, which is incorrect.
Step 2: Verify response method usage
The correct method to send a response string is res.send(). app.get('/hello', (req, res) => { res.write('Hello World'); }); uses res.write() which is not standard in Express, and app.get('/hello', (req, res) => { res.sendHello('Hello World'); }); uses a non-existent method res.sendHello().
B. Route path should be '/data/' with trailing slash
C. Cannot call res.send() twice in the same request handler
D. req parameter is unused and causes error
Solution
Step 1: Check response sending rules
Express allows sending only one response per request. Calling res.send() twice causes an error.
Step 2: Analyze other options
Missing semicolons do not cause runtime errors here, trailing slash is optional, and unused parameters do not cause errors.
Final Answer:
Cannot call res.send() twice in the same request handler -> Option C
Quick Check:
Only one res.send() per request allowed [OK]
Hint: Send response only once per route handler [OK]
Common Mistakes:
Calling res.send() multiple times
Thinking missing semicolons cause errors
Confusing trailing slash necessity
5. You want to create a GET route /product/:category/:id that responds with a message like "Category: electronics, ID: 123". Which code correctly implements this?
Step 1: Use correct parameter order and template literals
The callback must have (req, res) and use backticks with ${} to insert variables. app.get('/product/:category/:id', (req, res) => { res.send(`Category: ${req.params.category}, ID: ${req.params.id}`); }); does this correctly.
Step 2: Use req.params for URL parameters
Route parameters are accessed via req.params, not req.query. app.get('/product/:category/:id', (req, res) => { res.send(`Category: ${req.query.category}, ID: ${req.query.id}`); }); incorrectly uses req.query. app.get('/product/:category/:id', (req, res) => { res.send('Category: req.params.category, ID: req.params.id'); }); uses quotes, so variables won't be evaluated. app.get('/product/:category/:id', (res, req) => { res.send(`Category: ${req.params.category}, ID: ${req.params.id}`); }); swaps parameters.