Last active
December 22, 2015 03:38
-
-
Save dazld/6411353 to your computer and use it in GitHub Desktop.
working CLS example
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
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