Skip to content

Instantly share code, notes, and snippets.

@aigoncharov
Created February 28, 2019 20:54
Show Gist options
  • Save aigoncharov/b64a51d81128fd3f813067397480feeb to your computer and use it in GitHub Desktop.
Save aigoncharov/b64a51d81128fd3f813067397480feeb to your computer and use it in GitHub Desktop.
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