Bird
Raised Fist0
Expressframework~20 mins

GET route handling in Express - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Express GET Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output when accessing the GET route?

Consider this Express GET route handler:

app.get('/hello', (req, res) => {
  res.send('Hello World!');
});

What will the client receive when requesting /hello?

Express
app.get('/hello', (req, res) => {
  res.send('Hello World!');
});
AThe client receives an empty response with status 200.
BThe client receives a JSON object { message: 'Hello World!' }.
CThe client receives a 404 Not Found error.
DThe client receives the text 'Hello World!' as the response body.
Attempts:
2 left
💡 Hint

Look at what res.send does with a string argument.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a GET route with a URL parameter?

Which of the following Express GET route definitions correctly captures a URL parameter named id?

Aapp.get('/user/id', (req, res) => { res.send(req.params.id); });
Bapp.get('/user/:id', (req, res) => { res.send(req.params.id); });
Capp.get('/user/<id>', (req, res) => { res.send(req.params.id); });
Dapp.get('/user/:id?', (req, res) => { res.send(req.params.id); });
Attempts:
2 left
💡 Hint

URL parameters use a colon : before the name.

🔧 Debug
advanced
2:00remaining
Why does this GET route handler cause an error?

Examine this Express GET route handler:

app.get('/data', (req, res) => {
  const data = fetchData();
  res.send(data);
});

Assuming fetchData is an async function returning a Promise, why does this code cause an error or unexpected behavior?

Express
app.get('/data', (req, res) => {
  const data = fetchData();
  res.send(data);
});
ABecause res.send cannot be called inside a GET route handler.
BBecause fetchData is not defined, causing a ReferenceError.
CBecause fetchData returns a Promise, res.send sends the Promise object instead of the resolved data.
DBecause the route path '/data' is invalid syntax.
Attempts:
2 left
💡 Hint

Think about what happens when you call an async function without awaiting it.

❓ state_output
advanced
2:00remaining
What is the response when accessing this GET route multiple times?

Consider this Express code:

let count = 0;
app.get('/count', (req, res) => {
  count += 1;
  res.send(`Count is ${count}`);
});

What will the client see when requesting /count three times in a row?

Express
let count = 0;
app.get('/count', (req, res) => {
  count += 1;
  res.send(`Count is ${count}`);
});
AThe client sees 'Count is 1', then 'Count is 2', then 'Count is 3' on each request.
BThe client sees 'Count is undefined' because count is not initialized.
CThe client sees 'Count is 0' on the first request and errors on the next.
DThe client always sees 'Count is 1' on every request.
Attempts:
2 left
💡 Hint

Think about how the variable count changes with each request.

🧠 Conceptual
expert
2:00remaining
Which statement about Express GET route matching is true?

Given these routes:

app.get('/item/new', handler2);
app.get('/item/:id', handler1);

Which statement correctly describes how Express matches requests?

AA request to '/item/new' matches handler2 because exact routes have priority over parameter routes.
BA request to '/item/new' matches handler1 because parameter routes match before exact routes.
CA request to '/item/123' matches handler2 because it is defined second.
DA request to '/item/new' causes an error because routes conflict.
Attempts:
2 left
💡 Hint

Think about route specificity and matching order in Express.

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

  1. 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.
  2. 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.
  3. Final Answer:

    Defines a route to handle GET requests at a specific URL -> Option B
  4. 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

  1. 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.
  2. 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().
  3. Final Answer:

    app.get('/hello', (req, res) => { res.send('Hello World'); }); -> Option A
  4. Quick Check:

    Correct syntax uses req, res and res.send() [OK]
Hint: Use (req, res) and res.send() to respond in GET routes [OK]
Common Mistakes:
  • Swapping req and res parameters
  • Using res.write() instead of res.send()
  • Calling non-existent response methods
3. What will be the output when a GET request is made to /user/42 with this route handler?
app.get('/user/:id', (req, res) => {
  res.send(`User ID is ${req.params.id}`);
});
medium
A. User ID is undefined
B. User ID is :id
C. Error: req.params is not defined
D. User ID is 42

Solution

  1. Step 1: Understand route parameters

    The route /user/:id captures the part after /user/ as req.params.id.
  2. Step 2: Substitute parameter in response

    When visiting /user/42, req.params.id is "42", so the response is "User ID is 42".
  3. Final Answer:

    User ID is 42 -> Option D
  4. Quick Check:

    Route params fill req.params.id correctly [OK]
Hint: Use req.params to get URL parts in routes [OK]
Common Mistakes:
  • Using req.params without defining route parameter
  • Expecting literal ':id' in output
  • Forgetting to use backticks for template strings
4. Identify the error in this GET route handler:
app.get('/data', (req, res) => {
  res.send('Data received')
  res.send('Another response');
});
medium
A. Missing semicolon after first res.send()
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

  1. Step 1: Check response sending rules

    Express allows sending only one response per request. Calling res.send() twice causes an error.
  2. Step 2: Analyze other options

    Missing semicolons do not cause runtime errors here, trailing slash is optional, and unused parameters do not cause errors.
  3. Final Answer:

    Cannot call res.send() twice in the same request handler -> Option C
  4. 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?
hard
A. app.get('/product/:category/:id', (req, res) => { res.send(`Category: ${req.params.category}, ID: ${req.params.id}`); });
B. app.get('/product/:category/:id', (req, res) => { res.send('Category: req.params.category, ID: req.params.id'); });
C. app.get('/product/:category/:id', (res, req) => { res.send(`Category: ${req.params.category}, ID: ${req.params.id}`); });
D. app.get('/product/:category/:id', (req, res) => { res.send(`Category: ${req.query.category}, ID: ${req.query.id}`); });

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    app.get('/product/:category/:id', (req, res) => { res.send(`Category: ${req.params.category}, ID: ${req.params.id}`); }); -> Option A
  4. Quick Check:

    Use req.params and template literals correctly [OK]
Hint: Use req.params and backticks for dynamic route responses [OK]
Common Mistakes:
  • Swapping req and res parameters
  • Using string quotes instead of template literals
  • Using req.query instead of req.params for route params
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard