Last active
December 25, 2016 19:35
-
-
Save kumavis/f62593d0365c2cdaf39fc4e65a918419 to your computer and use it in GitHub Desktop.
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
// 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