How to get a /health
route in Strapi CMS
It can also be used for similar use cases of a route without any
/api
or/admin
prefix
-
Generate a plugin named
healthcheck
This will generate this folder
src/plugins/healthcheck
npm run strapi generate plugin Plugin name: healthcheck
-
Strip down the generate folder
To keep the plugin folder as lite as Strapi allows.
plugins └─ healthcheck ├─ README.md ├─ package.json ├─ server │ ├─ bootstrap.js │ └─ index.js └─ strapi-server.js
-
Strip down the
index.js
'use strict'; const bootstrap = require('./bootstrap'); module.exports = { bootstrap, };
-
Strip down and update the
package.json
{ "name": "healthcheck", "version": "0.0.0", "description": "First party app plugin to ensure /health route is available", "strapi": { "name": "Health Check", "description": "[1st Party] Ensures /health route is available", "kind": "plugin", "displayName": "Health Check" }, "maintainers": [ { "name": "Adedoyin Akande", } ], "engines": { "node": ">=18.0.0 <=20.x.x", "npm": ">=6.0.0" }, "license": "MIT" }
-
Update the
bootstrap.js
'use strict'; module.exports = ({ strapi }) => { const routes = [ { method: 'GET', path: `/health`, handler: (ctx) =>{ ctx.body = { "status": "ok", "message": "All engines running 🚀" } } } ]; routes.forEach(route => { route.info = { pluginName: 'healthcheck' }; route.config = { auth: false }; }); strapi.server.routes({ type: 'admin', routes }); };
-
Run
develop
orbuild
To test the new route
npm run develop