You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on May 12, 2021. It is now read-only.
Use koa-request-id as a middleware for a koa app. By default, it generates a unique uuid (v4) and exposes it on the response via the X-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:
Sometimes it is also useful to pass a custom id via a request header, specifically in tracking requests on the distributed system. 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 X-Request-Id header:
You can create a new request id middleware by passing the relevant options to requestId;
// With default optionsconstmiddleware=requestId({query: null,header: 'X-Request-Id',exposeHeader: 'X-Request-Id',generator: require('uuid/v4')});
Middleware Configuration
These are the available config options for the middleware. All is optional. The middleware try to get the request id from X-Request-Id request header or generate a new request id using uuidv4 generator. Then, the request id will be set into ctx.state.reqId Koa context state object and X-Request-Id response header if any option is not specified.
{// Request query name to get the forwarded request idquery: 'reqId',// Request header name to get the forwarded request idheader: 'X-Transaction-Id',// Response header nameexposeHeader: 'X-Transaction-Id',// Function to generate request idgenerator: ()=>Date.now().toString()}
Contributing
Bug Reports & Feature Requests
Please use the issue tracker to report any bugs or ask feature requests.