Skip to content

Instantly share code, notes, and snippets.

@ufocoder
Created October 31, 2018 10:58
Show Gist options
  • Select an option

  • Save ufocoder/1d7927cc638ae88a01a0159a1b8f3246 to your computer and use it in GitHub Desktop.

Select an option

Save ufocoder/1d7927cc638ae88a01a0159a1b8f3246 to your computer and use it in GitHub Desktop.
router.get("/some-file", function (req, res) {
const url = "./data/some-file.ext";
function handleError (err) {
res.writeHead(404);
res.end();
}
function handleSuccess (file) {
res.writeHead(200, { "Content-Type": "application/octet-stream" });
res.write(file);
res.end();
}
readFile(url, function (err, file) {
if (err) {
handleError(err);
} else {
handleSuccess(file);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment