fastify-healthcheck
Version:
Fastify Plugin to serve responses for health checks
48 lines (42 loc) • 1.74 kB
JavaScript
/*
* Copyright 2018-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const fastify = require('fastify')()
// register plugin with all its options shown but commented (as a sample)
fastify.register(require('../src/plugin'), {
// healthcheckUrl: '/custom-health',
// healthcheckUrlDisable: true,
// healthcheckUrlAlwaysFail: true,
// exposeUptime: true,
// underPressureOptions: { } // no under-pressure specific options set here
exposeUptime: true // enable, as a sample
})
// example to handle a sample home request to serve a static page, optional here
fastify.get('/', function (req, reply) {
const path = require('node:path')
const scriptRelativeFolder = path.join(__dirname, path.sep)
const fs = require('node:fs')
const stream = fs.createReadStream(path.join(scriptRelativeFolder, 'home.html'))
reply.type('text/html; charset=utf-8').send(stream)
})
fastify.listen({ port: 3000, host: '0.0.0.0' }, (err, address) => {
if (err) throw err
console.log(`Server listening on ${address}`)
})
fastify.ready(() => {
const routes = fastify.printRoutes()
console.log(`Available Routes:\n${routes}`)
})