Files
expressjs.com/zh-tw/advanced/healthcheck-graceful-shutdown.md
github-actions[bot] e4004e1a0a i18n: new crowdin translations (#2064)
Co-authored-by: Crowdin Bot <support+bot@crowdin.com>

Co-authored-by: bjohansebas <103585995+bjohansebas@users.noreply.github.com>
2025-12-13 21:43:55 -05:00

1.4 KiB

layout, title, description, menu, order, redirect_from
layout title description menu order redirect_from
page Health Checks and Graceful Shutdown Learn how to implement health checks and graceful shutdown in Express apps to enhance reliability, manage deployments, and integrate with load balancers like Kubernetes. advanced 5

Health Checks and Graceful Shutdown

Graceful shutdown

When you deploy a new version of your application, you must replace the previous version. The process manager you're using will first send a SIGTERM signal to the application to notify it that it will be killed. Once the application gets this signal, it should stop accepting new requests, finish all the ongoing requests, clean up the resources it used, including database connections and file locks then exit.

範例

const server = app.listen(port)

process.on('SIGTERM', () => {
  debug('SIGTERM signal received: closing HTTP server')
  server.close(() => {
    debug('HTTP server closed')
  })
})

Health checks

A load balancer uses health checks to determine if an application instance is healthy and can accept requests. For example, Kubernetes has two health checks:

  • liveness, that determines when to restart a container.
  • readiness, that determines when a container is ready to start accepting traffic. When a pod is not ready, it is removed from the service load balancers.