Microservice Healthcheck Snippet
code (typescript)
23 hours ago
·
33 lines
·
19 views
1import http from 'node:http';
3interface HealthResponse {
4 status: 'healthy' | 'degraded' | 'unhealthy';
5 uptimeSeconds: number;
6 memoryUsageMb: number;
7 timestamp: string;
8}
10export function getSystemHealth(): HealthResponse {
11 const memory = process.memoryUsage();
12 return {
13 status: 'healthy',
14 uptimeSeconds: Math.floor(process.uptime()),
15 memoryUsageMb: Math.round(memory.heapUsed / 1024 / 1024),
16 timestamp: new Date().toISOString(),
17 };
18}
20const server = http.createServer((req, res) => {
21 if (req.url === '/healthz' && req.method === 'GET') {
22 res.writeHead(200, { 'Content-Type': 'application/json' });
23 res.end(JSON.stringify(getSystemHealth(), null, 2));
24 return;
25 }
27 res.writeHead(404, { 'Content-Type': 'application/json' });
28 res.end(JSON.stringify({ error: 'Not Found' }));
29});
31server.listen(3000, () => {
32 console.log('Healthcheck service ready on port 3000');
33});
Replies 0
No replies yet
Every reply is a note. Start a discussion, ask a question, or attach a code snippet.
Notification