Microservice Healthcheck Snippet
typescriptimport http from 'node:http';
interface HealthResponse {
status: 'healthy' | 'degraded' | 'unhealthy';
uptimeSeconds: number;
memoryUsageMb: number;
timestamp: string;
}
export function getSystemHealth(): HealthResponse {
const memory = process.memoryUsage();
return {
status: 'healthy',
uptimeSeconds: Math.floor(process.uptime()),
memoryUsageMb: Math.round(memory.heapUsed / 1024 / 1024),
timestamp: new Date().toISOString(),
};
}
const server = http.createServer((req, res) => {
if (req.url === '/healthz' && req.method === 'GET') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(getSystemHealth(), null, 2));
return;
}
res.writeHead(404, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Not Found' }));
});
server.listen(3000, () => {
console.log('Healthcheck service ready on port 3000');
});