0
0
Expressframework~20 mins

Testing GET endpoints in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express GET Endpoint Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the response body of this GET endpoint?
Consider this Express GET endpoint. What will the client receive as the response body when requesting /hello?
Express
const express = require('express');
const app = express();

app.get('/hello', (req, res) => {
  res.json({ message: 'Hello, world!' });
});
A{"message":"Hello, world!"}
B"Hello, world!"
C{"msg":"Hello, world!"}
DStatus 404 Not Found
Attempts:
2 left
💡 Hint
Look at the method used to send the response and the object passed.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a GET endpoint that sends plain text?
Which code snippet correctly defines a GET endpoint at /text that sends the plain text response "Hello"?
Aapp.get('/text', (req, res) => { res.json('Hello'); });
Bapp.get('/text', (req, res) => { res.sendJson('Hello'); });
Capp.get('/text', (req, res) => { res.send('Hello'); });
Dapp.get('/text', (req, res) => { res.sendText('Hello'); });
Attempts:
2 left
💡 Hint
Check the Express method that sends plain text responses.
🔧 Debug
advanced
2:00remaining
Why does this GET endpoint cause a runtime error?
Examine the code below. Why will a GET request to /data cause a runtime error?
Express
app.get('/data', (req, res) => {
  const data = undefined;
  res.json(data.value);
});
ATypeError because data is undefined and accessing data.value fails
BReferenceError because res is not defined
CSyntaxError due to missing semicolon
DNo error, response is null
Attempts:
2 left
💡 Hint
What happens if you try to access a property of undefined?
state_output
advanced
2:00remaining
What is the response after multiple GET requests?
Given this Express app, what will be the response body of the third GET request to /count?
Express
let count = 0;
app.get('/count', (req, res) => {
  count += 1;
  res.json({ count });
});
A{"count":2}
B{"count":1}
C{"count":0}
D{"count":3}
Attempts:
2 left
💡 Hint
The variable count increases by 1 on each request.
🧠 Conceptual
expert
3:00remaining
Which option correctly explains why this GET endpoint never sends a response?
Consider this Express GET endpoint. Why does a client request to /never hang indefinitely?
Express
app.get('/never', (req, res) => {
  if (false) {
    res.send('Done');
  }
  // no else or res.send outside if
});
ABecause the if condition is false, res.send throws an error
BBecause res.send is never called, the server never ends the response, causing the client to wait forever
CBecause Express requires res.json instead of res.send
DBecause the route path is invalid and Express ignores the handler
Attempts:
2 left
💡 Hint
Think about what happens if no response method is called in Express.