Created
October 31, 2018 12:11
-
-
Save ufocoder/012d0cbdccf4dfea9407544fc30e14b2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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