Last active
December 25, 2016 19:35
Star
You must be signed in to star a gist
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
// 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