Skip to content

Instantly share code, notes, and snippets.

@prof3ssorSt3v3
Created October 6, 2019 20:07
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prof3ssorSt3v3/96e6b29cd5baf4aeaad29a93fd3c7ed5 to your computer and use it in GitHub Desktop.
Save prof3ssorSt3v3/96e6b29cd5baf4aeaad29a93fd3c7ed5 to your computer and use it in GitHub Desktop.
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