Express.js request ID and correlation ID middleware
JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
test
.gitignore
.travis.yml
LICENSE
README.md
package.json
requestId.js

README.md

Build Status Coverage Status npm version

request-id

Express.js request-id middleware.
Generates and sets a new request UUID in each request header (by default in request-id header).
Generates and sets a new correlation UUID if not already exists (by default in correlation-id header).
Responds with the remote ID if given (allows server clients to pass their own identifier).
Encapsulates an HTTP client (request) within the request object as req.service.request which by default will pass forward the remote ID (if given) and the correlation ID headers.

Installation

$ npm install cc-request-id

Running the tests

$ npm install
$ mocha

API

var requestId = require('cc-request-id')

requestId(options)

Create new request-id middleware.

options

secret Secret string for authenticating an incoming request correlation ID was generated from a trusted server holding the same secret.
namespace (optional) prefix for every generated request-id (and conatenated right after the request URL path name in correlation ID, if generated)
serviceSecretKey (optional) Key of the request header to be set for authenticating an incoming request correlation ID was generated from a trusted server holding the same secret.
requestIdKey (optional) Key of the request header to be set for the request ID.
correlationIdKey (optional) Key of the request header to be set for the correlation ID.
remoteIdKey (optional) Key of the request header to be set for the remote ID.

Example

var express = require('express')
var app = express()
var requestId = require('cc-request-id')
var bodyParser = require('body-parser')

app.use(requestId({secret: '1234', namespace: 'myServer'}))
app.use(bodyParser())
app.get('/test', function (req, res, next) {
    res.status(200).send({
        requestId: req.headers['request-id'],
        correlationId: req.headers['correlation-id']
    })
})
app.listen(8080)

test it:

curl http://localhost:8080/test

outputs:

requestId: myServer-32fd0631-5a10-4564-b8c7-f704be22f13a
correlationId: /test-myServer-32fd0631-5a10-4564-b8c7-f704be22f13a