Created
September 22, 2016 15:01
-
-
Save aarongustafson/bb763cae271714822c03508fae50ceec to your computer and use it in GitHub Desktop.
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
var render = function (encoding) { | |
return function (page, req, res) { | |
if (encoding && !req.acceptsEncodings(encoding)) { | |
return false; | |
} | |
var content = getContent(page, encoding); | |
if (content) { | |
res.set('Content-Encoding', encoding); | |
res.set('Content-Type', 'text/html'); | |
res.end(content, encoding ? 'binary' : null); | |
return true; | |
} | |
return false; | |
}; | |
}; | |
var renderers = [ | |
render('br'), | |
render('gz'), | |
render() | |
]; | |
var renderContent = function (page, req, res) { | |
return _.some(renderers, function (renderer) { | |
return renderer(page, req, res); | |
}); | |
}; | |
var showPage = function (req, res, next) { | |
var page = req.params.page; | |
if (!renderContent(page, req, res)) { | |
return next({ status: 404 }); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment