-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
45 lines (39 loc) · 1.42 KB
/
Copy pathexample.js
File metadata and controls
45 lines (39 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const express = require('express');
const catOrNot = require('./index');
const app = express();
// Example API endpoints that will be monitored
app.get('/api/status', (req, res) => {
res.json({ status: 'ok', service: 'api' });
});
app.get('/api/database', (req, res) => {
// Simulate database health check
const isHealthy = Math.random() > 0.2; // 80% chance of being healthy
if (isHealthy) {
res.json({ status: 'ok', service: 'database' });
} else {
res.status(500).json({ status: 'error', service: 'database' });
}
});
// Add the cat-or-not health monitoring
// Using inline configuration for this example
app.use(catOrNot({
endpoints: [
{ url: 'http://localhost:3000/api/status', method: 'GET' },
{ url: 'http://localhost:3000/api/database', method: 'GET' }
],
checkInterval: 10000, // Check every 10 seconds
timeout: 3000,
healthPath: '/health'
}));
// Start server
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Example server running on http://localhost:${PORT}`);
console.log('');
console.log('Try these endpoints:');
console.log(` - http://localhost:${PORT}/health (health check - shows cat if all healthy)`);
console.log(` - http://localhost:${PORT}/api/status (always healthy)`);
console.log(` - http://localhost:${PORT}/api/database (randomly fails)`);
console.log('');
console.log('Refresh the /health endpoint multiple times to see the cat appear and disappear!');
});