Bird
Raised Fist0
Expressframework~10 mins

Health check endpoints in Express - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a basic health check route that responds with 'OK'.

Express
app.get('/health', (req, res) => {
  res.status([1]).send('OK');
});
Drag options to blanks, or click blank then click option'
A404
B302
C500
D200
Attempts:
3 left
💡 Hint
Common Mistakes
Using 404 or 500 which indicate errors.
Forgetting to send a response.
2fill in blank
medium

Complete the code to add a JSON response with a status message in the health check endpoint.

Express
app.get('/health', (req, res) => {
  res.status(200).json({ status: [1] });
});
Drag options to blanks, or click blank then click option'
A'error'
B'ok'
C'fail'
D'pending'
Attempts:
3 left
💡 Hint
Common Mistakes
Using negative status messages like 'error' or 'fail'.
3fill in blank
hard

Fix the error in the health check route to correctly send a JSON response.

Express
app.get('/health', (req, res) => {
  res.send([1]);
});
Drag options to blanks, or click blank then click option'
A{ status: 'ok' }
B'OK'
C200
Dres.status(200)
Attempts:
3 left
💡 Hint
Common Mistakes
Sending a string instead of an object.
Passing a number or status code directly to send.
4fill in blank
hard

Fill both blanks to create a health check endpoint that responds with status 200 and JSON { status: 'healthy' }.

Express
app.get('/health', (req, res) => {
  res.status([1]).json({ status: [2] });
});
Drag options to blanks, or click blank then click option'
A200
B'healthy'
C'ok'
D500
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong status codes like 500.
Using 'ok' instead of 'healthy' for the status message.
5fill in blank
hard

Fill all three blanks to create a health check endpoint that responds with status 200, JSON { status: 'ok' }, and logs 'Health check passed' to the console.

Express
app.get('/health', (req, res) => {
  console.log([1]);
  res.status([2]).json({ status: [3] });
});
Drag options to blanks, or click blank then click option'
A'Health check passed'
B200
C'ok'
D'Server running'
Attempts:
3 left
💡 Hint
Common Mistakes
Logging wrong messages.
Using incorrect status codes or JSON values.

Practice

(1/5)
1. What is the main purpose of a health check endpoint in an Express app?
easy
A. To serve static files like images
B. To confirm the app is running and responsive
C. To handle user login requests
D. To connect to the database

Solution

  1. Step 1: Understand what a health check endpoint does

    A health check endpoint is a simple route that tells if the app is alive and working.
  2. Step 2: Compare options to this purpose

    Only To confirm the app is running and responsive matches this purpose. Other options are unrelated to health checks.
  3. Final Answer:

    To confirm the app is running and responsive -> Option B
  4. Quick Check:

    Health check = Confirm app running [OK]
Hint: Health check endpoints just say if app is alive [OK]
Common Mistakes:
  • Thinking health check handles user data
  • Confusing health check with static file serving
  • Assuming it manages database connections
2. Which of the following is the correct syntax to create a simple health check GET route in Express?
easy
A. app.listen('/health', () => console.log('OK'));
B. app.post('/health', (req, res) => res.send('OK'));
C. app.get('/health', (req, res) => res.send('OK'));
D. app.use('/health', (req, res) => res.send('OK'));

Solution

  1. Step 1: Identify the HTTP method for health check

    Health checks use GET requests to check status, so app.get is correct.
  2. Step 2: Check syntax correctness

    app.get('/health', (req, res) => res.send('OK')); uses app.get with path '/health' and sends 'OK' response correctly.
  3. Final Answer:

    app.get('/health', (req, res) => res.send('OK')); -> Option C
  4. Quick Check:

    GET route with res.send('OK') = app.get('/health', (req, res) => res.send('OK')); [OK]
Hint: Health checks use GET routes with res.send('OK') [OK]
Common Mistakes:
  • Using POST instead of GET
  • Using app.use instead of app.get
  • Trying to listen on a route path
3. What will be the output when a client sends a GET request to the following Express route?
app.get('/health', (req, res) => {
  res.status(200).send('Service is up');
});
medium
A. HTTP 200 OK with body 'Service is up'
B. HTTP 404 Not Found
C. HTTP 500 Internal Server Error
D. No response, request times out

Solution

  1. Step 1: Analyze the route handler

    The route listens for GET on '/health' and sends status 200 with text 'Service is up'.
  2. Step 2: Determine the HTTP response

    The client receives status 200 OK and the string 'Service is up' in the response body.
  3. Final Answer:

    HTTP 200 OK with body 'Service is up' -> Option A
  4. Quick Check:

    Status 200 + send text = HTTP 200 OK with body 'Service is up' [OK]
Hint: res.status(200).send('text') sends 200 OK with that text [OK]
Common Mistakes:
  • Assuming 404 if route exists
  • Confusing status codes
  • Expecting no response or error
4. Identify the error in this health check endpoint code and choose the fix:
app.get('/health', (req, res) => {
  res.sendStatus(200);
  res.send('OK');
});
medium
A. Remove res.send('OK') because res.sendStatus ends response
B. Change res.sendStatus(200) to res.status(200)
C. Add next() call after res.sendStatus(200)
D. Change app.get to app.post

Solution

  1. Step 1: Understand res.sendStatus behavior

    res.sendStatus(200) sends status and ends the response immediately.
  2. Step 2: Identify why res.send('OK') causes error

    Calling res.send after response ended causes an error. So remove the second send.
  3. Final Answer:

    Remove res.send('OK') because res.sendStatus ends response -> Option A
  4. Quick Check:

    One response per request = Remove res.send('OK') because res.sendStatus ends response [OK]
Hint: Only send one response per request [OK]
Common Mistakes:
  • Sending multiple responses in one route
  • Confusing res.sendStatus and res.status
  • Adding next() unnecessarily
5. You want your Express app's health check endpoint to return JSON with status and uptime in seconds. Which code correctly implements this?
hard
A. app.get('/health', (req, res) => { res.json('status: ok, uptime: ' + process.uptime()); });
B. app.get('/health', (req, res) => { res.send('status: ok, uptime: ' + process.uptime()); });
C. app.get('/health', (req, res) => { res.status(200).send({ status: 'ok', uptime: process.uptime() }); });
D. app.get('/health', (req, res) => { res.json({ status: 'ok', uptime: process.uptime() }); });

Solution

  1. Step 1: Understand JSON response method

    res.json sends a JSON object with correct headers automatically.
  2. Step 2: Check each option's response format

    app.get('/health', (req, res) => { res.json({ status: 'ok', uptime: process.uptime() }); }); sends a JSON object with keys 'status' and 'uptime' correctly. app.get('/health', (req, res) => { res.send('status: ok, uptime: ' + process.uptime()); }); sends a string, not JSON. app.get('/health', (req, res) => { res.status(200).send({ status: 'ok', uptime: process.uptime() }); }); sends an object with res.send but may not set JSON headers properly. app.get('/health', (req, res) => { res.json('status: ok, uptime: ' + process.uptime()); }); sends a string inside res.json, which is incorrect.
  3. Final Answer:

    app.get('/health', (req, res) => { res.json({ status: 'ok', uptime: process.uptime() }); }); -> Option D
  4. Quick Check:

    Use res.json with object for JSON response [OK]
Hint: Use res.json({key: value}) to send JSON response [OK]
Common Mistakes:
  • Sending JSON as string instead of object
  • Using res.send with object without headers
  • Confusing res.json and res.send
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