Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

fastify-healthcheck

smartiniOnGitHub84.2kApache-2.05.1.0TypeScript support: included

Fastify Plugin to serve responses for health checks

fastify, plugin, healthcheck

readme

fastify-healthcheck

NPM Version NPM Downloads Code Style

Fastify Plugin to serve responses that report about the web application, if it's still running and alive (health checks).

This is very useful with Containers like Docker and orchestrators like Kubernetes.

With this plugin, Fastify by default expose an healthcheck route configured for /health GET requests, and even a script that can be executed to get content via HTTP GET from that running web application.

Usage

The plugin can be used without specifying options, so good default values will be used, but if needed can be specified:

  • healthcheckUrl, to set a different uri for the healthcheck route
  • healthcheckUrlDisable, to not publish the healthcheck route
  • healthcheckUrlAlwaysFail, to always return failure responses (useful to test failure responses)
  • exposeUptime, to return even Node.js process uptime (by default disabled)
  • underPressureOptions, for options to send directly to under-pressure
  • schemaOptions, for options to use for route schema (no default value provided)

Under the hood, the healthcheck status is determined by the @fastify/under-pressure plugin, used here as a dependency; so it's possible to specify all its configuration options in related option.

To use all default values for healthcheck options, do not set its options (or set with undefined values); in that way no under-pressure specific options will be overridden by them.

Sample usage:

const fastify = require('fastify')()

// example without specifying options, returning a default healthcheck
// route mapped to '/health' that only reply to a GET request
fastify.register(require('fastify-healthcheck'))
// or
// example with custom healthcheck url and response to always fail
// fastify.register(require('fastify-healthcheck'), { healthcheckUrl: '/custom-health', healthcheckUrlAlwaysFail: true })
//

fastify.listen({ port: 3000, host: 'localhost' })

// To test, for example (in another terminal session) do:
// `npm start`, or
// `curl http://127.0.0.1:3000/health` => returning an HTTP response 200 (OK)
// and a JSON response like: {"statusCode":200,"status":"ok"}
// or run the healthcheck script, for example with:
// `node src/healthcheck http://localhost:3000/health`
// and get the same HTTP response seen before

In the example folder there is a simple server scripts that uses the plugin (inline but it's the same using it from npm registry).

The file Dockerfile.example is a sample container definition for the example webapp (using the plugin) to show Docker HEALTHCHECK directive both using 'curl' (but commented) and calling the healthcheck script available by the plugin. For convenience, all Docker commands have been defined in package.json, to run many of them in a simple way (with npm run custom-command), like in the following sequence:

  • docker:build, to build the image, where the entry point is the example
  • docker:build:fail, to build the image, but as entry point the example that is triggering the Service Unavailable error (HTTP 503) in the healthcheck route
  • docker:run, to start the container from generated image, in detached mode
  • docker:healthcheck-manual, to run the healthcheck script in the container but manually
  • docker:status, to get the health status of the container
  • and others like: docker:inspect (interactive), docker:log (<CTRL>C to close), docker:process, etc ...
  • docker:stop, to stop running container
  • docker:clean, to remove generated image

Requirements

Fastify ^5.0.0 , Node.js 20 LTS (20.9.0) or later. Note that plugin releases 4.x are for Fastify 4.x, 5.x for Fastify 5.x, etc.

Sources

Source code is all inside main repo: fastify-healthcheck.

Documentation generated from source code (library API): here.

Note

To fully encapsulate under-pressure features inside the scope of this plugin, the plugin is not exposed by fastify-plugin; for more info look here, here.

The plugin map a default endpoint on the URI /health to be called via GET, but it's possible to change it with the setting 'url' in plugin options.

The plugin exposes even another script that tries to get some content (via HTTP GET) from the current web application where it's running. In a container environment this could be useful to let containers runtime do the healthcheck without the need to use other tools like curl or wget that must be available in the container.

Both approaches could be useful in most common cases, like Kubernetes HTTP GET, or Kubernetes EXEC or Docker HEALTHCHECK, or others similar.

Note that the healthcheck script gets the URL to call from the command-line, but if not specified it will use a default value of http://localhost:3000/health.

To execute the healthcheck script from another Node.js project/package, you need to run something like: node node_modules/fastify-healthcheck/src/healthcheck http://localhost:8000/health, with the webapp exposed to the port 8000 in this case.

License

Licensed under Apache-2.0.


changelog

Change Log

5.1.0 (2024-11-09)

Full Changelog Summary Changelog:

  • Add plugin configOptions argument to allow passing config to route
  • Updated all dependencies to latest

5.0.0 (2024-09-22)

Full Changelog Summary Changelog:

  • Updated requirements to Fastify '^5.0.0'
  • Updated all dependencies to latest (for Node.js 20 LTS)

4.4.0 (2023-01-06)

Full Changelog Summary Changelog:

  • Updated requirements to Fastify '^4.11.0' and all other dependencies to latest
  • Adds schema options for health endpoint

4.3.0 (2022-12-12)

Full Changelog Summary Changelog:

  • Updated requirements to Fastify '^4.10.2' and under-pressure '^8.2.0'; updated all other dependencies to latest
  • Compatibility with TypeScript 4.9 and NodeNext / ESNext

4.2.0 (2022-09-01)

Full Changelog Summary Changelog:

  • Updated requirements to Fastify '^4.5.3' and under-pressure '^8.1.0'
  • Compatibility with TypeScript 4.8
  • Improve plugin source docs, with JSDoc
  • Simplified/updated plugin tests

4.1.0 (2022-07-19)

Full Changelog Summary Changelog:

  • Updated requirements to Fastify '^4.0.1'
  • Ensure all is good as before

4.0.0 (2022-06-13)

Full Changelog Summary Changelog:

  • Updated requirements to Fastify '^4.0.0'; update code with related changes
  • Updated all dependencies to latest (for Node.js 14 LTS)
  • Update dependencies from 'under-pressure' to the new (scoped) package '@fastify/under-pressure'
  • Update and simplified example and test code
  • Update example Docker files to use Node.js slim image as base to reduce image size and all is good
  • Update documentation from sources with JSDoc

3.2.0 (2022-06-13)

Full Changelog Summary Changelog:

  • Updated requirements to Fastify '3.11.0' or higher (but still 3.x)
  • Updated all dependencies to latest (for Node.js 10 LTS)
  • Update Copyright year
  • Update Tap configuration and small simplifications in tests
  • Generate documentation from sources with JSDoc

3.1.0 (2021-02-09)

Full Changelog Summary Changelog:

  • Updated requirements to Under-pressure '^5.6.0'
  • Update all dependencies to latest, and removed 'standardx' (as dev dependency)
  • Fix some (now) failing tests
  • Fix exposure of uptime after requested the first time
  • Ensure compatibility with Under-pressure option to specify a custom route for healthcheck/status

3.0.0 (2020-07-24)

Full Changelog Summary Changelog:

  • Updated requirements to Fastify '^3.0.0' (as dev dependency)
  • Updated all dependencies to latest
  • Update TypeScript types

2.3.1 (2020-06-01)

Summary Changelog:

  • Fix TypeScript types

2.3.0 (2020-05-26)

Summary Changelog:

  • Add TypeScript types
  • Update requirements to Fastify '^2.14.1' (as dev dependency)
  • Update dependency on 'under-pressure' 4.x in this release (latest for Fastify 2.x)
  • Update other dev dependencies

2.2.0 (2020-05-24)

Summary Changelog:

  • Add a new plugin flag 'exposeUptime' (by default false, so disabled) to add even Node.js process uptime in the reply, when enabled
  • Updated requirements to Fastify '^2.8.0' (as dev dependency)
  • Keep dependency on 'under-pressure' 3.x in this release
  • Updated other dev dependencies

2.1.0 (2019-05-08)

Summary Changelog:

  • Updated requirements to Fastify '^2.1.0'
  • Updated to latest 'under-pressure', which drops support for Node.js 6
  • Updated all dependencies

2.0.0 (2019-02-26)

Summary Changelog:

  • Updated requirements to Fastify 2.x
  • Updated all dependencies
  • Note that this release number is aligned with 'under-pressure' 2.x, so for Fastify v2

1.0.0 (2019-02-03)

Summary Changelog:

  • Updated all dependencies
  • Updated Tap tests
  • Note that this release number means that the plugin is stable, and aligned with 'under-pressure' 1.x, and Fastify v1

0.3.0 (2019-01-26)

Summary Changelog:

  • Update under-pressure to '1.x' and update all dev dependencies to latest release
  • Note that the minor release change here is to let consumers of this package to have more evidence of underlying library release changes

0.2.0 (2018-11-27)

Summary Changelog:

  • Delegate to the under-pressure plugin the logic to report the status of the web application
  • To fully encapsulate under-pressure features, removed the dependency on fastify-plugin; for more info look here, here
  • Update Fastify dependencies to '1.1.0' or higher (but on 1.x), but without fastify-plugin to check it now (see related changes)
  • Add another example (example-under-pressure-fail) with under-pressure configured to always return a Service Unavailable error (HTTP 503), and related Dockerfile-fail.example to simplify its usage (as a sample)
  • Update healthcheck standalone script to return 0 for success, or the HTTP error code in case of failure (or 1 in case of other failure)

0.1.1 (2018-11-13)

Summary Changelog:

  • Maintenance release to fix Fastify dependencies to '1.x', to avoid breaking changes because Fastify '2.x' will be released soon
  • Updated dependencies to latest Fastify plugin (1.2.1) and Fastify 1.x (1.13.0)

0.1.0 (2018-10-31)

Summary Changelog:

  • First release, with basic features: expose an healthcheck route, add a script to check the health status by calling that route via HTTP GET
  • Ability to override some features, with plugin options
  • Add a sample webapp using it
  • Add a sample Dockerfile with the HEALTHCHECK directive
  • Add sample Docker commands to show and simplify its usage
  • Add some documentation