Skip to content

Instantly share code, notes, and snippets.

@dazld
Forked from tooxie/cls-example.js
Last active December 22, 2015 03:59
Show Gist options
  • Save dazld/6414160 to your computer and use it in GitHub Desktop.
Save dazld/6414160 to your computer and use it in GitHub Desktop.
var local = require('./namespace-module');
var http = require('http');
var uuid = require('uuid').v4;
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 id = local.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');
var cls = require('continuation-local-storage-glue');
var ns = cls.getNamespace('nsApp');
if(!ns) ns = cls.createNamespace('nsApp');
module.exports = ns;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment