Created
October 6, 2019 20:07
-
-
Save prof3ssorSt3v3/96e6b29cd5baf4aeaad29a93fd3c7ed5 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
const http = require("http"); | |
const fs = require("fs"); | |
const server = http.createServer(function(req, res) { | |
let img = __dirname + "/pod.jpg"; | |
fs.access(img, fs.constants.F_OK, err => { | |
//check that we can access the file | |
console.log(`${img} ${err ? "does not exist" : "exists"}`); | |
}); | |
fs.readFile(img, function(err, content) { | |
if (err) { | |
res.writeHead(404, { "Content-type": "text/html" }); | |
res.end("<h1>No such image</h1>"); | |
} else { | |
//specify the content type in the response will be an image | |
res.writeHead(200, { "Content-type": "image/jpg" }); | |
res.end(content); | |
} | |
}); | |
}); | |
server.listen(1234, function() { | |
console.log("Server running on port 1234"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment