Bird
Raised Fist0
Node.jsframework~20 mins

Health check endpoints in Node.js - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Health Check Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
1:30remaining
What is the output of this simple health check endpoint?
Consider this Node.js Express health check endpoint. What response does the server send when a GET request is made to /health?
Node.js
import express from 'express';
const app = express();
app.get('/health', (req, res) => {
  res.status(200).json({ status: 'ok' });
});
// Assume server listens correctly
A{"status":"ok"}
BStatus code 404 with empty body
CStatus code 500 with error message
DPlain text response 'ok' with status 200
Attempts:
2 left
💡 Hint
Look at the response method used and the status code set.
📝 Syntax
intermediate
1:30remaining
Which option causes a syntax error in this health check endpoint?
Identify which code snippet will cause a syntax error when defining a health check endpoint in Express.
Aapp.get('/health', (req, res) => { res.status(200).json({ status: 'ok' ) }); });
Bapp.get('/health', (req, res) => res.json({ status: 'ok' }));
Capp.get('/health', (req, res) => { res.send('OK'); });
Dapp.get('/health', function(req, res) { res.sendStatus(200); });
Attempts:
2 left
💡 Hint
Look carefully at the brackets and parentheses in the JSON response.
❓ state_output
advanced
2:00remaining
What is the output when the health check endpoint uses async function but forgets to await?
Given this health check endpoint, what will the client receive when requesting /health?
Node.js
app.get('/health', async (req, res) => {
  const status = checkDatabase(); // async function returning a promise
  res.json({ dbStatus: status });
});

async function checkDatabase() {
  return 'connected';
}
AEmpty JSON object {}
B{"dbStatus":"connected"}
CStatus code 500 with error
D{"dbStatus":{}}
Attempts:
2 left
💡 Hint
Remember what happens if you don't await a promise before sending it in JSON.
🔧 Debug
advanced
2:00remaining
Why does this health check endpoint cause the server to hang?
Examine this health check endpoint code. Why does the server never respond to requests on /health?
Node.js
app.get('/health', (req, res) => {
  checkServiceStatus();
});

function checkServiceStatus() {
  // some async operation but no callback or promise handling
  setTimeout(() => {
    console.log('Service is up');
  }, 1000);
}
AThe console.log throws an error stopping the response.
BThe setTimeout blocks the event loop causing the hang.
CThe endpoint never calls res.send or res.json, so the client waits forever.
DThe endpoint returns a promise but does not handle rejection.
Attempts:
2 left
💡 Hint
Think about what the server must do to finish a request.
🧠 Conceptual
expert
1:30remaining
Which option best describes the purpose of a health check endpoint in a Node.js service?
Choose the most accurate description of why health check endpoints are used in backend services.
ATo authenticate users before allowing access to the main application.
BTo provide a simple URL that external systems can call to verify the service is running and responsive.
CTo expose detailed internal metrics and logs for debugging purposes.
DTo serve the main content of the application to users.
Attempts:
2 left
💡 Hint
Think about monitoring and uptime checks.

Practice

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

Solution

  1. Step 1: Understand the role of health check endpoints

    Health check endpoints are designed to verify if the application is alive and can respond to requests.
  2. Step 2: Differentiate from other endpoints

    Endpoints for login, static files, or database connections serve different purposes unrelated to health checks.
  3. Final Answer:

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

    Health check = confirms app is alive [OK]
Hint: Health check endpoints confirm app status quickly [OK]
Common Mistakes:
  • Confusing health check with user authentication
  • Thinking health check serves static content
  • Assuming health check manages database connections
2. Which of the following is the correct way to define a simple health check endpoint using Express in Node.js?
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 checks

    Health checks typically use GET requests to allow simple status queries.
  2. Step 2: Check Express syntax for route definition

    Using app.get with a path and a callback sending a response is the correct pattern.
  3. Final Answer:

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

    GET route with res.send('OK') = correct syntax [OK]
Hint: Health checks use GET routes with res.send [OK]
Common Mistakes:
  • Using POST instead of GET for health checks
  • Using app.use which matches all methods
  • Confusing app.listen with route handling
3. Given this Express code snippet, what will the response be when a client requests /health?
const express = require('express');
const app = express();
app.get('/health', (req, res) => {
  res.json({ status: 'ok', uptime: process.uptime() });
});
app.listen(3000);
medium
A. No response because the route is not defined
B. A plain text 'ok' message
C. An error because res.json is not a function
D. A JSON object with status 'ok' and uptime in seconds

Solution

  1. Step 1: Analyze the route handler

    The route '/health' uses res.json to send a JSON object with keys 'status' and 'uptime'.
  2. Step 2: Understand process.uptime()

    process.uptime() returns the number of seconds the Node.js process has been running, a number value.
  3. Final Answer:

    A JSON object with status 'ok' and uptime in seconds -> Option D
  4. Quick Check:

    res.json sends JSON = JSON object response [OK]
Hint: res.json sends JSON object, not plain text [OK]
Common Mistakes:
  • Thinking res.json is undefined
  • Expecting plain text instead of JSON
  • Assuming route is missing or broken
4. Identify the error in this health check endpoint code and choose the fix:
const express = require('express');
const app = express();
app.get('/health', (req, res) => {
  res.sendStatus(200).send('OK');
});
app.listen(3000);
medium
A. Replace res.sendStatus(200).send('OK') with res.status(200).send('OK')
B. Change app.get to app.post
C. Remove res.send('OK') and keep only res.sendStatus(200)
D. Add next parameter and call next() after res.sendStatus

Solution

  1. Step 1: Understand res.sendStatus behavior

    res.sendStatus(200) sends the status and ends the response, so chaining .send('OK') causes an error.
  2. Step 2: Use correct method chaining

    res.status(200).send('OK') sets status and sends body correctly without ending response prematurely.
  3. Final Answer:

    Replace res.sendStatus(200).send('OK') with res.status(200).send('OK') -> Option A
  4. Quick Check:

    res.status().send() chains properly, res.sendStatus() ends response [OK]
Hint: Use res.status().send() to set status and send body [OK]
Common Mistakes:
  • Chaining send after sendStatus causes error
  • Changing HTTP method unnecessarily
  • Calling next() after sending response
5. You want to create a health check endpoint that returns JSON with status as 'ok' only if the database connection is alive, otherwise status is 'error'. Which approach correctly implements this in Node.js with Express?
hard
A. app.get('/health', (req, res) => { checkDbConnection().then(() => res.json({ status: 'ok' })); });
B. app.get('/health', async (req, res) => { const dbAlive = await checkDbConnection(); res.json({ status: dbAlive ? 'ok' : 'error' }); });
C. app.get('/health', (req, res) => { const dbAlive = checkDbConnection(); res.json({ status: dbAlive ? 'ok' : 'error' }); });
D. app.get('/health', (req, res) => { res.json({ status: 'ok' }); });

Solution

  1. Step 1: Recognize async database check

    Checking database connection is asynchronous, so await or then() must be used to get result.
  2. Step 2: Validate correct async usage

    app.get('/health', async (req, res) => { const dbAlive = await checkDbConnection(); res.json({ status: dbAlive ? 'ok' : 'error' }); }); uses async/await properly to wait for checkDbConnection result before sending JSON response.
  3. Final Answer:

    Use async function with await to check DB and respond accordingly -> Option B
  4. Quick Check:

    Async/await needed for async DB check [OK]
Hint: Use async/await to handle async DB checks in health endpoint [OK]
Common Mistakes:
  • Calling async function without await or then
  • Ignoring DB status and always returning 'ok'
  • Not handling promise results properly
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