Skip to content

Instantly share code, notes, and snippets.

@aarongustafson
Created September 22, 2016 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aarongustafson/bb763cae271714822c03508fae50ceec to your computer and use it in GitHub Desktop.
Save aarongustafson/bb763cae271714822c03508fae50ceec to your computer and use it in GitHub Desktop.
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