Skip to content

Commit 836cb57

Browse files
committed
add health check endpoint for server
1 parent 8a87a18 commit 836cb57

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict'
2+
3+
const Router = require('koa-router')
4+
const mongoose = require('mongoose')
5+
6+
let router = new Router()
7+
8+
router
9+
.get('/health-check', async (ctx) => {
10+
const state = mongoose.connection.readyState
11+
// When state === 0, it means the connection is disconnected
12+
if (!state) {
13+
throw new Error('connection to mongodb is broken')
14+
}
15+
16+
ctx.status = 204
17+
ctx.body = null
18+
})
19+
20+
21+
module.exports = router

0 commit comments

Comments
 (0)