Nodejs Health Check API that responds with a JSON object based on the health status of all other backend dependencies.
In this project, we define an express app that listens on port 3005
. The app has one endpoint /health
with GET
method.
The backendDependencies
array holds the names and URLs of all the backend dependencies that we want to check for health status.
When a GET request
is made to /health
, the server iterates over all backend dependencies and makes a GET request to their /health endpoint. If the request is successful, the dependency is marked as "OK"
in the healthStatus object, otherwise it is marked as "Error"
.
After all APIs have been checked, the isHealthy variable is set to true if all APIs are "OK", otherwise it is set to false.
Finally, the server responds with a JSON object containing the isHealthy
flag and an array of results for each backend API.
This API allows for GET query requests and responds with a JSON object based on the health status of all backend dependencies. It can be used to quickly check the status of all backend dependencies and take appropriate action if any of them are not functioning correctly.
The dbConfig
object holds the configuration information for our Aurora DB.
When a GET request
is made to /db/health
, the server attempts to create a connection to the Aurora DB using the mysql2/promise library. If the connection is successful, the server runs a simple SELECT 1
query to check the health of the database. If the query is successful, the server returns a JSON object with a status of "OK", otherwise it returns a JSON object with a status of "Error"
.
git clone https://github.com/jayantapaul-18/health-check.git
cd health-check
npm install
node index.js
or,
npm install -g nodemon
nodemon index.js
pre-commit installs git hooks configured in .pre-commit-config.yaml
Install pre-commit
and commitizen
to use
brew install commitizen
brew install pre-commit
pre-commit install
pre-commit install --hook-type commit-msg
pre-commit run --all-files
git add .
git status
pre-commit run --all-files
cz c
git commit -m 'feat: health check api with backend api status response'
git push origin main --force
Run the below command to create a new release CHANGELOGS
npm install --save-dev standard-version
npm install --save-dev commitizen
npx commitizen init cz-conventional-changelog --save-dev --save-exact
# Install commitlint cli and conventional config
npm install --save-dev @commitlint/config-conventional @commitlint/cli
# Install Husky - Husky improves commits and more
npm install husky --save-dev
# Activate hooks
npx husky install
# Add hook
npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'
# run
npm run commit
npm run release
git push --follow-tags origin main && npm publish
We can bypass pre-commit and commit-msg hooks using Git -n/--no-verify option:
git commit -m "my no commit" --no-verify
curl --location 'http://localhost:3005/health?tls=0'
curl --location 'http://localhost:3005/health?tls=1'
Response
{
"isHealthy": true,
"node_tls_reject_unauthorize": "0",
"timeStamp": "2023-05-20T23:58:46.262Z",
"healthStatus": [
{
"i": "✅",
"name": "API 1",
"status": "OK",
"uptime": 49
},
{
"i": "✅",
"name": "API 2",
"status": "OK",
"uptime": 20
},
{
"i": "✅",
"name": "Backend-Node",
"status": "OK",
"uptime": 475
}
]
}
❯ docker build -t health-check .
- Need to optimize - TO DO
tree -L 1
├── CHANGELOG.md
├── CODEOWNERS
├── Dockerfile
├── LICENSE
├── README.md
├── REQUEST.http
├── app.log
├── commitlint.config.js
├── config.js
├── index.js
├── node_modules
├── package-lock.json
├── package.json
├── prepare.js
└── swagger.json