-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpress-mw.ts
More file actions
31 lines (27 loc) · 880 Bytes
/
express-mw.ts
File metadata and controls
31 lines (27 loc) · 880 Bytes
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
// To run demo: npx ts-node demo/express-mw.ts
import express from 'express'
import * as healthz from '../dist/index'
const app = express()
app.use((req, res, next) => {
healthz.express({
// Define checks
checks: [
{
id: 'PostgreSQL',
required: true,
fn: async () => new Promise((resolve) => setTimeout(resolve, 1000)),
},
{
id: 'Redis',
fn: async () => new Promise((resolve) => setTimeout(resolve, 10)),
},
],
// Make timeout configurable from the outside
timeout: parseInt(String(req.query.timeout)) || undefined,
// Modify the result if needed
transformResult: (x) => (console.log(x), x),
})(req, res, next)
})
const running = app.listen(process.env.PORT ?? 0)
const port = (running.address() as any).port
console.log(`Express app running on http://localhost:${port}/healthz`)