Skip to content

Instantly share code, notes, and snippets.

@aeadedoyin
Created March 6, 2024 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aeadedoyin/a67966c5aab50cec9de1403a26d3769a to your computer and use it in GitHub Desktop.
Save aeadedoyin/a67966c5aab50cec9de1403a26d3769a to your computer and use it in GitHub Desktop.
Health Check route /health in Strapi CMS

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

  1. Generate a plugin named healthcheck

    This will generate this folder src/plugins/healthcheck

    npm run strapi generate plugin
    
    Plugin name: healthcheck
  2. 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
  3. Strip down the index.js

    'use strict';
    
    const bootstrap = require('./bootstrap');
    
    module.exports = {
      bootstrap,
    };
  4. 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"
    }
    
  5. 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
      });
    };
  6. Run develop or build

    To test the new route

    npm run develop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment