📖 Scenario: You are building a simple Node.js server to monitor the health of your web service. Health check endpoints help other systems know if your service is running well.
🎯 Goal: Create a Node.js server with two health check endpoints: /health and /ready. The /health endpoint returns a simple status message. The /ready endpoint returns a readiness message.
📋 What You'll Learn
Create a Node.js server using the built-in http module.
Create a variable called port set to 3000.
Add a request listener function that checks the URL path.
Respond with JSON messages for /health and /ready endpoints.
Start the server listening on the specified port.
💡 Why This Matters
🌍 Real World
Health check endpoints are used by monitoring tools and load balancers to check if your service is running and ready to receive traffic.
💼 Career
Knowing how to create health check endpoints is important for backend developers and DevOps engineers to ensure reliable and maintainable services.
Progress0 / 4 steps
1
Create the server and port variable
Create a variable called port and set it to 3000. Then create a server using http.createServer() and assign it to a variable called server.
Node.js
Hint
Use const port = 3000; and const server = http.createServer(); to start.
2
Add a request listener function
Add a request listener function to server using server.on('request', (req, res) => { ... }). Inside the function, create a variable called url and set it to req.url.
Node.js
Hint
Use server.on('request', (req, res) => { const url = req.url; }) to handle requests.
3
Add core logic for health and ready endpoints
Inside the request listener, use if and else if statements to check if url is '/health' or '/ready'. For /health, respond with status code 200 and JSON {"status": "ok"}. For /ready, respond with status code 200 and JSON {"ready": true}. For other URLs, respond with status code 404 and JSON {"error": "Not found"}. Remember to set the Content-Type header to application/json before sending the response.
Node.js
Hint
Use if and else if to check url and respond with JSON and status codes.
4
Start the server listening on the port
Add a line to start the server listening on the port variable using server.listen(port).
Node.js
Hint
Use server.listen(port) to start the server.
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
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.
Step 2: Differentiate from other endpoints
Endpoints for login, static files, or database connections serve different purposes unrelated to health checks.
Final Answer:
To confirm the app is running and responsive -> Option A
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
Step 1: Identify the HTTP method for health checks
Health checks typically use GET requests to allow simple status queries.
Step 2: Check Express syntax for route definition
Using app.get with a path and a callback sending a response is the correct pattern.
Final Answer:
app.get('/health', (req, res) => res.send('OK')) -> Option C
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?
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?