0
0
Node.jsframework~10 mins

Health check endpoints in Node.js - Interactive Code Practice

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

Complete the code to create a basic health check endpoint using Express.

Node.js
const express = require('express');
const app = express();

app.get('/health', (req, res) => {
  res.status([1]).send('OK');
});

app.listen(3000);
Drag options to blanks, or click blank then click option'
A404
B200
C500
D302
Attempts:
3 left
💡 Hint
Common Mistakes
Using 404 or 500 status codes which indicate errors.
Forgetting to send a response.
2fill in blank
medium

Complete the code to send a JSON response with status and uptime in the health check endpoint.

Node.js
app.get('/health', (req, res) => {
  res.status(200).json({
    status: 'ok',
    uptime: process.[1]()
  });
});
Drag options to blanks, or click blank then click option'
Auptime
BmemoryUsage
CcpuUsage
Denv
Attempts:
3 left
💡 Hint
Common Mistakes
Using memoryUsage or cpuUsage which return different info.
Trying to access a property instead of calling a method.
3fill in blank
hard

Fix the error in the health check middleware to properly handle asynchronous checks.

Node.js
app.get('/health', async (req, res) => {
  const dbHealthy = await checkDatabaseConnection();
  if (!dbHealthy) {
    res.status([1]).json({ status: 'error' });
  } else {
    res.status(200).json({ status: 'ok' });
  }
});
Drag options to blanks, or click blank then click option'
A200
B500
C404
D503
Attempts:
3 left
💡 Hint
Common Mistakes
Using 200 status code even when the database is down.
Using 404 which means not found.
4fill in blank
hard

Fill both blanks to create a reusable health check middleware that sends JSON status and sets correct HTTP status code.

Node.js
function healthCheckMiddleware(req, res) {
  const healthy = checkSystemHealth();
  res.status([1]).json({ status: [2] });
}
Drag options to blanks, or click blank then click option'
A200
B'ok'
D503
Attempts:
3 left
💡 Hint
Common Mistakes
Using numeric status code as string or vice versa.
Sending status code 503 when system is healthy.
5fill in blank
hard

Fill all three blanks to create an advanced health check endpoint that checks database, cache, and returns appropriate status.

Node.js
app.get('/health', async (req, res) => {
  const dbOk = await checkDB();
  const cacheOk = await checkCache();
  if (dbOk && cacheOk) {
    res.status([1]).json({ status: [2] });
  } else {
    res.status([3]).json({ status: 'error' });
  }
});
Drag options to blanks, or click blank then click option'
A200
B'ok'
C503
D'healthy'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong status codes for failure.
Returning wrong status string for success.