(opts?: { round?: boolean })
| 4 | * Add X-Response-Time header field. |
| 5 | */ |
| 6 | export function ping(opts?: { round?: boolean }) { |
| 7 | return function responseTime(_: Request, res: Response, next: (err?: any) => void): void { |
| 8 | const start = process.hrtime() |
| 9 | |
| 10 | const delta = process.hrtime(start) |
| 11 | |
| 12 | // Format to high resolution time with nano time |
| 13 | let time = delta[0] * 1000 + delta[1] / 1000000 |
| 14 | |
| 15 | if (!opts?.round) time = Math.round(time) |
| 16 | |
| 17 | res.setHeader('X-Response-Time', `${time}ms`) |
| 18 | |
| 19 | next() |
| 20 | } |
| 21 | } |