0
0
Expressframework~20 mins

GET route handling in Express - Practice Problems & Coding Challenges

Choose your learning style9 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.