Skip to content

Instantly share code, notes, and snippets.

@kumavis
Last active December 25, 2016 19:35
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save kumavis/f62593d0365c2cdaf39fc4e65a918419 to your computer and use it in GitHub Desktop.
// example middleware
engine.use(function *(req, res, next) {
// make changes to request
// eg: check cache hit
yield next;
// read the result
// eg: populate cache
});
// cache middleware
engine.use(function *(req, res, next) {
// check cache hit
let idString = generateIdForReq(req)
let cacheResult = yield cacheGet(idString)
if (cacheResult) res.result = cacheResult
yield next;
// populate cache
if (!cacheResult) {
yield cachePut(idString, res.result)
}
});
// perf measuring middleware
engine.use(function *(req, res, next) {
var start = new Date();
// wait for request result to bubble back up
yield next;
// log elapsed time
var ms = new Date() - start;
console.log('%s %s %s - %sms', req.method, req.params, req.result, ms);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment