https://github.com/mitscherlich/koa2-servertime
Add `server-timing` headers to your koa.js server.
https://github.com/mitscherlich/koa2-servertime
koa server-timing
Last synced: about 2 months ago
JSON representation
Add `server-timing` headers to your koa.js server.
- Host: GitHub
- URL: https://github.com/mitscherlich/koa2-servertime
- Owner: Mitscherlich
- License: mit
- Created: 2022-07-15T11:54:41.000Z (almost 4 years ago)
- Default Branch: dev
- Last Pushed: 2022-08-08T16:20:22.000Z (almost 4 years ago)
- Last Synced: 2025-01-30T08:08:24.702Z (over 1 year ago)
- Topics: koa, server-timing
- Language: JavaScript
- Homepage: https://npm.im/koa2-servertime
- Size: 38.1 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# koa2-servertime
[](https://npm.im/koa2-servertime) [](https://npm.im/koa2-servertime)
## Install
via `pnpm`, `yarn` or `npm`:
```bash
pnpm add koa2-servertime
# or
yarn add koa2-servertime
# or
npm i -S koa2-servertime
```
## Usage
Simply add this middleware to your koa application:
```js
const Koa = require('koa')
const ServerTime = require('koa2-servertime')
const app = new Koa()
app.use(ServerTime())
app.use(async (ctx) => {
// async
await ctx.startTimePromise('api', 'Fetch Data', fetch(/* ... */))
await ctx.startTimePromise('db', 'Query Database', db.query(/* ... */))
await ctx.startTimePromise('cache', 'Get from Cache', cache.get(/* ... */))
// synchronous
ctx.startTime('render', 'Render Content')
ctx.body = render(/* ... */)
ctx.endTime('render')
})
```
This middleware will give you three apis for collecting timing information:
- `ctx.startTimePromise`: start a timer and return a promise, ended automatically when the promise settled.
- `ctx.startTime`: start a timer
- `ctx.endTime`: end a timer
If you want to disable/enable this middleware, you can simply set `enabled` property to `false` or `true`.
For example, only enable it for development:
```js
app.use(ServerTime({
enabled: process.env.NODE_ENV !== 'production',
}))
```
This middleware will automatically add a `Total Response Time` header to the response. You can disable or modify it with options.
```js
app.use(ServerTime({
total: true, // by default
totalName: 'total', // by default
totalDesc: 'Total Response Time', // by default
}))
```
For more detail, checkout [./example/app.js](./example/app.js).
### Options
- `enabled`: enable/disable this middleware. default: `true`
- `total`: enable/disable total response time. default: `true`
- `totalName`: total response time header name. default: `'total'`
- `totalDesc`: total response time header description. default: `'Total Response Time'`
## License
MIT © [Mitscherlich](https://mitscherlich.me)