Allows you to identify client requests within non-sequential logs such as Syslog by adding a response header of
X-Request-Id
. Allows setting value via query parameter or request header. For Koa and Express.
$ npm install request-id
# koa
var requestId = require('request-id');
var app = require('koa')();
app.use(requestId());
# express
var requestId = require('request-id/express');
var app = require('express')();
app.use(requestId());
% curl example.com
//=> X-Client-ID: a37cacc3-71d5-40f0-a329-a051a3949ced
% curl -H 'X-Request-ID:a37cacc3-71d5-40f0-a329-a051a3949ced' example.com
//=> X-Request-ID: a37cacc3-71d5-40f0-a329-a051a3949ced
% curl example.com?requestId=a37cacc3-71d5-40f0-a329-a051a3949ced
//=> X-Request-ID: a37cacc3-71d5-40f0-a329-a051a3949ced
app.use(requestId({
resHeader: 'X-Client-ID'
}));
% curl example.com
//=> X-Client-ID: a37cacc3-71d5-40f0-a329-a051a3949ced
Keep in mind that changing the request header does not have any effect on the default response header. If you want them to match, you'll need to set both
.resHeader
and.reqHeader
.
app.use(requestId({
reqHeader: 'X-Client-ID'
}));
% curl -H 'X-Client-ID:a37cacc3-71d5-40f0-a329-a051a3949ced' example.com
//=> X-Request-ID: a37cacc3-71d5-40f0-a329-a051a3949ced
app.use(requestId({
paramName: 'requestId'
}));
% curl example.com?requestId=a37cacc3-71d5-40f0-a329-a051a3949ced
//=> X-Request-ID: a37cacc3-71d5-40f0-a329-a051a3949ced
Custom Value Generator Function (default: node-uuid.v4)
app.use(requestId({
generator: function () { return 'ABC'; }
}));
% curl example.com
//=> X-Request-ID: ABC