Last active
June 24, 2016 15:17
-
-
Save akhoury/0434240fd4383753a997221e4685f114 to your computer and use it in GitHub Desktop.
cls-nodebb-testcase
This file contains 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
// this file is assumed to be in the root NodeBB directory | |
var continuationLocalStorage = require('continuation-local-storage'); | |
var cls = continuationLocalStorage.createNamespace('test'); | |
var async = require('async'); | |
var path = require('path'); | |
var nconf = require('nconf'); | |
var express = require('express'); | |
var app = express(); | |
nconf.file({file: './config.json' }); | |
var dbType = nconf.get('database'); | |
var productionDbConfig = nconf.get(dbType); | |
var db = require('./src/database'); | |
if (! db.client) { | |
nconf.set(dbType, productionDbConfig); | |
db.init(onDatabaseInit); | |
} else { | |
onDatabaseInit(); | |
} | |
function onDatabaseInit () { | |
app.use(clsMiddleware); | |
app.get('/', onIndex); | |
app.listen(3000, function () { | |
console.log('app listening on port 3000!'); | |
}); | |
} | |
function clsMiddleware (req, res, next) { | |
cls.run(function() { | |
cls.set('foo', 'bar'); | |
cls.set('request', req) | |
next(); | |
}); | |
} | |
function onIndex (req, res) { | |
async.waterfall([ | |
function (next) { | |
// comment this line and it stops working | |
return db.getObject('user:1', cls.bind(next)); | |
db.getObject('user:1', next); | |
} | |
], function () { | |
res.send({ | |
result: cls.get('foo') ? "OK" : "NOT-OK", | |
isClsRequestAvailable: !!cls.get('request') | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment