Created
February 28, 2019 20:54
-
-
Save aigoncharov/b64a51d81128fd3f813067397480feeb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const cls = require('cls-hooked') | |
const uuidv4 = require('uuid/v4') | |
const clsNamespace = cls.createNamespace('app') | |
const clsMiddleware = (req, res, next) => { | |
// req and res are event emitters. We want to access CLS context inside of their event callbacks | |
clsNamespace.bind(req) | |
clsNamespace.bind(res) | |
const traceID = uuidv4() | |
clsNamespace.run(() => { | |
clsNamespace.set('traceID', traceID) | |
next() | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment