fastify-healthcheck
Version:
Fastify Plugin to serve responses for health checks
51 lines (44 loc) • 1.82 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.
*/
// example web application, but with under-pressure configured
// to always return a `Service Unavailable` error (HTTP 503)
const fastify = require('fastify')()
fastify.register(require('../src/plugin'), {
underPressureOptions: {
maxEventLoopDelay: 100,
maxHeapUsedBytes: 1,
// maxRssBytes: 2,
message: 'Under pressure!', // sample custom message
retryAfter: 50
} // set some under-pressure specific options set here
}) // configure this plugin with some custom options
// 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}`)
})