Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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: 5 days 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 (over 2 years ago)
- Default Branch: dev
- Last Pushed: 2022-08-08T16:20:22.000Z (over 2 years ago)
- Last Synced: 2024-12-16T00:11:16.502Z (19 days 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
[![npm version](https://badgen.net/npm/v/koa2-servertime)](https://npm.im/koa2-servertime) [![npm downloads](https://badgen.net/npm/dm/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 timerIf 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)