Skip to content

Instantly share code, notes, and snippets.

@ufocoder
Created October 31, 2018 12:11
Show Gist options
  • Select an option

  • Save ufocoder/012d0cbdccf4dfea9407544fc30e14b2 to your computer and use it in GitHub Desktop.

Select an option

Save ufocoder/012d0cbdccf4dfea9407544fc30e14b2 to your computer and use it in GitHub Desktop.
function closeWithStatus (response, status, headers) {
const data = headers || {};
function end () {
response.writeHead(status, data);
response.end();
}
return end;
}
function closeWithContent (response, status, headers) {
const data = headers || {};
function endWith (content) {
response.writeHead(status, data);
response.write(content);
response.end();
}
return endWith;
}
function higherOrderNodeCallback (onSuccess, onError) {
return function nodeCallback (err, content) {
if (err) {
onError(err);
} else {
onSuccess(content);
}
};
}
router.get(url, function (req, res) {
const headers = { "Content-Type": "application/octet-stream" };
const onSuccess = closeWithContent(res, 200, headers);
const onError = closeWithStatus(res, 404);
const callback = higherOrderNodeCallback(onSuccess, onError);
readFile(filePath, callback);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment