uphold/koa-requestid

Name: koa-requestid

Owner: Uphold

Description: A middleware that adds a request id to a koa application

Created: 2015-07-24 01:28:43.0

Updated: 2017-07-18 17:16:38.0

Pushed: 2017-03-23 11:28:21.0

Homepage: null

Size: 50

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

koa-requestid

Status

npm version build status

Installation

Install the package via yarn:

rn add koa-requestid

or via npm:

m install koa-requestid --save
Usage

Use koa-requestid as a middleware for a koa app. By default, it generates a unique uuid (v4) and exposes it on the response via the Request-Id header. The id is also saved as part of the request state.

In the following example, the generated uuid is manually exposed on the body for debugging purposes:

t Koa = require('koa');
t requestId = require('koa-requestid');
t app = new Koa();

use(requestId());
use(async ctx => {
x.body = ctx.state.id;


listen(3000);

Execute a request to the running app:

rl -v http://localhost:3000

TP/1.1 200 OK
quest-Id: cc0f12c7-f3b6-4c86-94c2-8c4ce7751651

12c7-f3b6-4c86-94c2-8c4ce7751651

Sometimes it is also useful to pass a custom id via a request header or query string, specifically in debugging sessions. Please note that the input id is not sanitized, so the usual precautions apply.

Using the above snippet to send a custom via the default Request-Id header:

rl -v -H 'Request-Id: foobar' http://localhost:3000

TP/1.1 200 OK
quest-Id: foobar

ar

or using a query string parameter (default is requestId):

rl -v http://localhost:3000?requestId=foobar

TP/1.1 200 OK
quest-Id: foobar

ar
Configuration
Arguments
  1. options (object): A dictionary of options.
Options
  1. expose (boolean|string): The name of the header to expose the id on the response, or false to disable.
  2. header (boolean|string): The name of the header to read the id on the request, or false to disable.
  3. query (boolean|string): The name of the header to read the id on the query string, or false to disable.

Example:

t Koa = require('koa');
t requestId = require('koa-requestid');
t app = new Koa();

use(requestId({
pose: 'X-Request-Id',
ader: 'X-Req-Id',
ery: 'request-id'

This configuration would expose every generated request id via the X-Request-Id response header and accept a custom id via the X-Req-Id header or request-id query string parameter.

Tests
rn test
Release
version [<new version> | major | minor | patch] -m "Release %s"
License

MIT


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.