A GET route in Express is a way to handle HTTP GET requests from clients. It defines what the server should do when a user visits a specific URL.
Click to reveal answer
beginner
How do you define a GET route for the path '/home' in Express?
You use app.get('/home', (req, res) => { res.send('Hello Home'); }); to define a GET route that sends 'Hello Home' when '/home' is visited.
Click to reveal answer
beginner
What do the req and res objects represent in a GET route handler?
req is the request object containing information from the client, like query data. res is the response object used to send data back to the client.
Click to reveal answer
intermediate
How can you send JSON data in response to a GET request in Express?
Use res.json({ key: 'value' }) inside the GET route handler to send JSON data back to the client.
Click to reveal answer
beginner
Why is it important to handle GET routes properly in a web server?
Proper GET route handling ensures users get the right content when they visit URLs. It helps the server respond quickly and correctly, improving user experience.
Click to reveal answer
Which method defines a GET route in Express?
Aapp.get()
Bapp.post()
Capp.put()
Dapp.delete()
✗ Incorrect
The app.get() method is used to define a GET route in Express.
In Express, what does the res.send() method do inside a GET route?
ASends a response back to the client
BReceives data from the client
CStarts the server
DDefines a new route
✗ Incorrect
res.send() sends a response back to the client.
What object holds information about the client's request in a GET route handler?
Ares
Bserver
Capp
Dreq
✗ Incorrect
The req object contains information about the client's request.
How do you send JSON data in response to a GET request?
Ares.sendFile()
Bres.json()
Cres.redirect()
Dres.render()
✗ Incorrect
Use res.json() to send JSON data in response.
Which URL path will this route handle? app.get('/about', ...)
A/
B/home
C/about
D/contact
✗ Incorrect
The route handles GET requests to the '/about' path.
Explain how to create a simple GET route in Express that returns a welcome message.
Think about how you tell the server what to do when someone visits a URL.
You got /4 concepts.
Describe the roles of the req and res objects in a GET route handler.
One listens to the client, the other talks back.
You got /3 concepts.
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.