Health check endpoints help you quickly see if your web app is running well. They give a simple answer to say "I am alive and working."
0
0
Health check endpoints in Express
Introduction
You want to monitor if your server is up and responding.
You need to check your app's status automatically in a cloud or hosting service.
You want to let load balancers know if your app can handle traffic.
You want a simple URL to test your app's basic health during development.
Syntax
Express
app.get('/health', (req, res) => { res.status(200).send('OK'); });
This creates a GET route at '/health' that sends back a 200 status and 'OK' text.
Use simple responses so checks are fast and reliable.
Examples
Basic health check that returns 'OK' with status 200.
Express
app.get('/health', (req, res) => { res.status(200).send('OK'); });
Health check returning JSON with a status message.
Express
app.get('/health', (req, res) => { res.json({ status: 'healthy' }); });
Health check that also returns how long the app has been running.
Express
app.get('/health', (req, res) => { const uptime = process.uptime(); res.json({ status: 'healthy', uptime: uptime }); });
Sample Program
This Express app listens on port 3000 and has a health check endpoint at '/health'. When you visit '/health', it responds with 'OK' and status 200.
Express
import express from 'express'; const app = express(); const PORT = 3000; app.get('/health', (req, res) => { res.status(200).send('OK'); }); app.listen(PORT, () => { console.log(`Server running on http://localhost:${PORT}`); });
OutputSuccess
Important Notes
Keep health check endpoints simple and fast to avoid slowing your app.
Use status code 200 to indicate healthy, other codes for problems.
Make sure health checks do not require authentication so monitoring tools can access them easily.
Summary
Health check endpoints tell if your app is running and ready.
They are simple GET routes that return a quick response like 'OK'.
Use them to help monitoring, load balancing, and debugging.