Skip to content

Instantly share code, notes, and snippets.

@dazld
Last active December 22, 2015 03:38
Show Gist options
  • Save dazld/6411353 to your computer and use it in GitHub Desktop.
Save dazld/6411353 to your computer and use it in GitHub Desktop.
working CLS example
var cls = require('continuation-local-storage-glue');
var express = require('express');
var app = express();
var v4 = require('uuid').v4;
var local = cls.createNamespace('reqNS');
app.use(function(req,res,next){
req._id = v4();
local.run(function(){
local.set('name', req._id );
req.name = local.get('name');
next();
});
});
app.get('*',function(req,res){
setTimeout(function(){
var ns = cls.getNamespace('reqNS');
var id = ns.get('name');
console.log(id == req._id);
res.send('test: ' + (id == req._id));
},Math.random() * 3300);
});
app.listen(3030);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment