GET route handling lets your server respond when someone asks for information. It helps deliver web pages or data when users visit a link.
GET route handling in Express
app.get('/path', (req, res) => { // code to handle the request res.send('response') })
app.get listens for GET requests on the given path.
The callback has req for request info and res to send back a response.
app.get('/', (req, res) => { res.send('Welcome to the homepage!') })
app.get('/about', (req, res) => { res.send('About us page') })
app.get('/user/:id', (req, res) => { const userId = req.params.id res.send(`User ID requested: ${userId}`) })
This program creates a simple Express server with two GET routes. The root route sends a greeting message. The second route reads a name from the URL and sends a personalized greeting.
import express from 'express' const app = express() const port = 3000 app.get('/', (req, res) => { res.send('Hello! This is the homepage.') }) app.get('/greet/:name', (req, res) => { const name = req.params.name res.send(`Hello, ${name}! Welcome to our site.`) }) app.listen(port, () => { console.log(`Server running at http://localhost:${port}`) })
GET routes only respond to GET requests, which are usually for getting data or pages.
Use req.params to get dynamic parts of the URL like IDs or names.
Always send a response with res.send(), res.json(), or similar to avoid hanging requests.
GET route handling lets your server answer requests for pages or data.
Use app.get(path, callback) to define what happens when a URL is visited.
You can read URL parts with req.params to customize responses.
Practice
app.get() method do in an Express app?Solution
Step 1: Understand the purpose of app.get()
Theapp.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 likeapp.post()handle POST requests, andapp.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 BQuick Check:
app.get() defines GET routes [OK]
- Confusing app.get() with app.listen()
- Thinking app.get() sends requests instead of handling them
- Mixing GET with POST methods
/hello that sends "Hello World" as a response?Solution
Step 1: Check parameter order in callback
The callback function parameters must bereqfirst, thenres. 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 isres.send(). app.get('/hello', (req, res) => { res.write('Hello World'); }); usesres.write()which is not standard in Express, and app.get('/hello', (req, res) => { res.sendHello('Hello World'); }); uses a non-existent methodres.sendHello().Final Answer:
app.get('/hello', (req, res) => { res.send('Hello World'); }); -> Option AQuick Check:
Correct syntax uses req, res and res.send() [OK]
- Swapping req and res parameters
- Using res.write() instead of res.send()
- Calling non-existent response methods
/user/42 with this route handler?app.get('/user/:id', (req, res) => {
res.send(`User ID is ${req.params.id}`);
});Solution
Step 1: Understand route parameters
The route/user/:idcaptures the part after/user/asreq.params.id.Step 2: Substitute parameter in response
When visiting/user/42,req.params.idis "42", so the response is "User ID is 42".Final Answer:
User ID is 42 -> Option DQuick Check:
Route params fill req.params.id correctly [OK]
- Using req.params without defining route parameter
- Expecting literal ':id' in output
- Forgetting to use backticks for template strings
app.get('/data', (req, res) => {
res.send('Data received')
res.send('Another response');
});Solution
Step 1: Check response sending rules
Express allows sending only one response per request. Callingres.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 CQuick Check:
Only one res.send() per request allowed [OK]
- Calling res.send() multiple times
- Thinking missing semicolons cause errors
- Confusing trailing slash necessity
/product/:category/:id that responds with a message like "Category: electronics, ID: 123". Which code correctly implements this?Solution
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 viareq.params, notreq.query. app.get('/product/:category/:id', (req, res) => { res.send(`Category: ${req.query.category}, ID: ${req.query.id}`); }); incorrectly usesreq.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.Final Answer:
app.get('/product/:category/:id', (req, res) => { res.send(`Category: ${req.params.category}, ID: ${req.params.id}`); }); -> Option AQuick Check:
Use req.params and template literals correctly [OK]
- Swapping req and res parameters
- Using string quotes instead of template literals
- Using req.query instead of req.params for route params
Ready to prove it?
Pass the test and this pattern moves unknown
