Skip to content

Instantly share code, notes, and snippets.

@kumavis kumavis/engine2.js

Last active Dec 25, 2016
What would you like to do?
// 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
You can’t perform that action at this time.