Skip to content

Instantly share code, notes, and snippets.

@aheinze
Forked from respectTheCode/static_server.js
Last active February 21, 2016 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aheinze/6745006 to your computer and use it in GitHub Desktop.
Save aheinze/6745006 to your computer and use it in GitHub Desktop.
var http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs"),
port = process.argv[2] || 3000,
mimeTypes = {
'asc' : 'text/plain',
'au' : 'audio/basic',
'avi' : 'video/x-msvideo',
'bin' : 'application/octet-stream',
'class' : 'application/octet-stream',
'css' : 'text/css',
'csv' : 'application/vnd.ms-excel',
'doc' : 'application/msword',
'dll' : 'application/octet-stream',
'dvi' : 'application/x-dvi',
'exe' : 'application/octet-stream',
'htm' : 'text/html',
'html' : 'text/html',
'json' : 'application/json',
'js' : 'application/x-javascript',
'txt' : 'text/plain',
'bmp' : 'image/bmp',
'rss' : 'application/rss+xml',
'atom' : 'application/atom+xml',
'gif' : 'image/gif',
'jpeg' : 'image/jpeg',
'jpg' : 'image/jpeg',
'jpe' : 'image/jpeg',
'png' : 'image/png',
'ico' : 'image/vnd.microsoft.icon',
'mpeg' : 'video/mpeg',
'mpg' : 'video/mpeg',
'mpe' : 'video/mpeg',
'qt' : 'video/quicktime',
'mov' : 'video/quicktime',
'wmv' : 'video/x-ms-wmv',
'mp2' : 'audio/mpeg',
'mp3' : 'audio/mpeg',
'rm' : 'audio/x-pn-realaudio',
'ram' : 'audio/x-pn-realaudio',
'rpm' : 'audio/x-pn-realaudio-plugin',
'ra' : 'audio/x-realaudio',
'wav' : 'audio/x-wav',
'zip' : 'application/zip',
'pdf' : 'application/pdf',
'xls' : 'application/vnd.ms-excel',
'ppt' : 'application/vnd.ms-powerpoint',
'wbxml' : 'application/vnd.wap.wbxml',
'wmlc' : 'application/vnd.wap.wmlc',
'wmlsc' : 'application/vnd.wap.wmlscriptc',
'spl' : 'application/x-futuresplash',
'gtar' : 'application/x-gtar',
'gzip' : 'application/x-gzip',
'swf' : 'application/x-shockwave-flash',
'tar' : 'application/x-tar',
'xhtml' : 'application/xhtml+xml',
'snd' : 'audio/basic',
'midi' : 'audio/midi',
'mid' : 'audio/midi',
'm3u' : 'audio/x-mpegurl',
'tiff' : 'image/tiff',
'tif' : 'image/tiff',
'rtf' : 'text/rtf',
'wml' : 'text/vnd.wap.wml',
'wmls' : 'text/vnd.wap.wmlscript',
'xsl' : 'text/xml',
'xml' : 'text/xml',
'svg' : 'image/svg+xml'
};
http.createServer(function(request, response) {
var uri = url.parse(request.url).pathname, filename = path.join(process.cwd(), uri);
fs.exists(filename, function(exists) {
if(!exists) {
response.writeHead(404, {"Content-Type": "text/plain"});
response.end("404 Not Found");
return;
}
if (fs.statSync(filename).isDirectory()) filename += '/index.html';
fs.readFile(filename, "binary", function(err, file) {
if(err) {
response.writeHead(500, {"Content-Type": "text/plain"});
response.end(err);
return;
}
var ext = filename.replace(/.*[\.\/\\]/, '').toLowerCase();
response.writeHead(200, {"Content-Type": (mimeTypes[ext] || "text/plain")});
response.end(file, "binary");
});
});
}).listen(parseInt(port, 10));
console.log("Static server running at : http://localhost:" + port + "/\nCTRL + C to shutdown");
@TWiStErRob
Copy link

consider ext = path.extname(filename).substring(1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment