|
| 1 | +# @eggjs/status |
| 2 | + |
| 3 | +Health check middleware for Egg applications, migrated from [egg-status](https://github.com/eggjs/egg-status). |
| 4 | + |
| 5 | +This package supports Egg 4 and Node.js >= 22.18.0. Applications on Egg 3 can continue to use `egg-status`. |
| 6 | + |
| 7 | +## Install |
| 8 | + |
| 9 | +```bash |
| 10 | +npm i @eggjs/status |
| 11 | +``` |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +Enable the plugin in `config/plugin.ts`: |
| 16 | + |
| 17 | +```ts |
| 18 | +import statusPlugin from '@eggjs/status'; |
| 19 | + |
| 20 | +export default { |
| 21 | + ...statusPlugin(), |
| 22 | +}; |
| 23 | +``` |
| 24 | + |
| 25 | +The package declaration is also supported: |
| 26 | + |
| 27 | +```ts |
| 28 | +export default { |
| 29 | + status: { |
| 30 | + enable: true, |
| 31 | + package: '@eggjs/status', |
| 32 | + }, |
| 33 | +}; |
| 34 | +``` |
| 35 | + |
| 36 | +The middleware runs immediately after `meta`. Requests to `/egg.status` return HTTP `200` with an `online` keyword when `${baseDir}/egg.status` exists. Otherwise, they return HTTP `404` with an `offline` keyword. The response also includes the application name, middleware start time, and current time. Other paths continue to the application's middleware and routes. |
| 37 | + |
| 38 | +## Configuration |
| 39 | + |
| 40 | +The defaults are: |
| 41 | + |
| 42 | +```ts |
| 43 | +// config/config.default.ts |
| 44 | +import path from 'node:path'; |
| 45 | +import type { EggAppInfo, PartialEggConfig } from 'egg'; |
| 46 | +import '@eggjs/status'; |
| 47 | + |
| 48 | +export default (appInfo: EggAppInfo): PartialEggConfig => ({ |
| 49 | + status: { |
| 50 | + availableResponseKeyword: 'online', |
| 51 | + unavailableResponseKeyword: 'offline', |
| 52 | + unavailableResponseStatus: 404, |
| 53 | + checkStatusURLs: ['/egg.status'], |
| 54 | + checkStatusFiles: [path.join(appInfo.baseDir, 'egg.status')], |
| 55 | + }, |
| 56 | +}); |
| 57 | +``` |
| 58 | + |
| 59 | +`checkStatusURLs` matches exact request paths; query strings do not affect matching. The application is available when **any** configured status file is accessible. File contents are ignored. If no file is accessible, including when the file list is empty, the middleware returns the configured unavailable response. Files are checked on each request, so creating or removing a file changes the response without a restart. |
| 60 | + |
| 61 | +The package exports `StatusConfig` for TypeScript consumers. |
| 62 | + |
| 63 | +## Deployment |
| 64 | + |
| 65 | +Use [@eggjs/scripts](../../tools/scripts/README.md) to start and stop the application: |
| 66 | + |
| 67 | +```bash |
| 68 | +npm i @eggjs/scripts |
| 69 | +``` |
| 70 | + |
| 71 | +On a POSIX system, create `egg.status` after startup and remove it before shutdown: |
| 72 | + |
| 73 | +```json |
| 74 | +{ |
| 75 | + "scripts": { |
| 76 | + "start": "egg-scripts start --daemon && touch egg.status", |
| 77 | + "stop": "rm -f egg.status && sleep 15 && egg-scripts stop" |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +Configure the load balancer to check `/egg.status` and accept HTTP `200`. Adjust the shutdown delay to allow the load balancer to stop sending traffic before the application exits. |
| 83 | + |
| 84 | +## Migration from egg-status |
| 85 | + |
| 86 | +Replace the `egg-status` dependency with `@eggjs/status` and update `config/plugin.ts` as shown above. The `status` configuration keys, default health path, status file, and response format are unchanged. |
| 87 | + |
| 88 | +## Contributing |
| 89 | + |
| 90 | +See the [contributing guide](../../CONTRIBUTING.md). Report bugs and suggestions in the [issue tracker](https://github.com/eggjs/egg/issues). |
| 91 | + |
| 92 | +## License |
| 93 | + |
| 94 | +[MIT](LICENSE) |
0 commit comments