Skip to content

Instantly share code, notes, and snippets.

@bitnetwork
Created March 7, 2017 23:30
Show Gist options
  • Save bitnetwork/780087aedfcdabf22aff01a000646cdd to your computer and use it in GitHub Desktop.
Save bitnetwork/780087aedfcdabf22aff01a000646cdd to your computer and use it in GitHub Desktop.
Android created Gist
var lib_http = require("http");
var lib_fs = require("fs");
var lib_path = require("path");
var lib_websocket = require("websocket");
var lib_chalk = require("chalk");
var https = lib_http.createServer(function(request, response) {
var filePath = "." + request.url;
if (filePath === "./") {
filePath = "./index.html";
}
var extname = lib_path.extname(filePath);
var contentType = "text/html";
switch (extname) {
case ".js":
contentType = "text/javascript";
break;
case ".css":
contentType = "text/css";
break;
case ".json":
contentType = "application/json";
break;
case ".png":
contentType = "image/png";
break;
case ".jpg":
contentType = "image/jpg";
break;
case ".wav":
contentType = "audio/wav";
break;
}
lib_fs.readFile(filePath, function(error, content) {
if (error) {
if (error.code === "ENOENT"){
lib_fs.readFile("./404.html", function(error, content) {
response.writeHead(404, { "Content-Type": contentType });
response.end(content, "utf-8");
});
} else {
response.writeHead(500);
response.end("Sorry, check with the site admin for error: "+error.code+" ..\n");
response.end();
}
} else {
response.writeHead(200, { "Content-Type": contentType });
response.end(content, "utf-8");
}
});
});
https.listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment