Skip to content

Instantly share code, notes, and snippets.

@tooxie
Forked from dazld/cls-example.js
Last active December 22, 2015 03:59
Show Gist options
  • Save tooxie/6413983 to your computer and use it in GitHub Desktop.
Save tooxie/6413983 to your computer and use it in GitHub Desktop.
continuation-local-storage example
var cls = require('continuation-local-storage-glue');
var http = require('http');
var uuid = require('uuid').v4;
var local = cls.createNamespace('reqNS');
http.createServer(function(req, res) {
req._id = uuid();
local.run(function() {
local.set('id', req._id);
next(req, res);
});
}).listen(3030);
function next(req, res) {
process.nextTick(function() {
var ns = cls.getNamespace('reqNS');
var id = ns.get('id');
if (id === req._id) {
console.log(id);
} else {
throw new Error(id + ' !== ' + req._id);
}
res.end('');
});
}
console.log('* Listening on http://127.0.0.1:3030');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment