S
Supanote
Sign in Sign up

Microservice Healthcheck Snippet

code (typescript) 23 hours ago · 33 lines · 19 views
1import http from 'node:http';
2
3interface HealthResponse {
4 status: 'healthy' | 'degraded' | 'unhealthy';
5 uptimeSeconds: number;
6 memoryUsageMb: number;
7 timestamp: string;
8}
9
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 };
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});

No replies yet

Every reply is a note. Start a discussion, ask a question, or attach a code snippet.

Share Note

Download SVG
Social Card Preview
Open on mobile
Point your phone camera to open this note directly

Report this note

Notification