Skip to content

Instantly share code, notes, and snippets.

@PierceLBrooks
Last active November 16, 2024 22:46
Show Gist options
  • Save PierceLBrooks/b3389a2ca790b05ced89f2e4d5940680 to your computer and use it in GitHub Desktop.
Save PierceLBrooks/b3389a2ca790b05ced89f2e4d5940680 to your computer and use it in GitHub Desktop.
"use strict";
var http = require('http');
var url = require('url');
var fs = require('fs');
var mimetypes = {
'.dvb' : 'application/dvbsubs',
'.srt' : 'application/x-subrip',
'.mks' : 'application/x-matroska',
'.ssa' : 'text/x-ssa',
'.ass' : 'text/x-ssa',
'.vtt' : 'text/vtt',
'.sub' : 'text/vnd.dvb.subtitle',
'.mka' : 'audio/x-matroska',
'.oga' : 'audio/ogg',
'.opus' : 'audio/ogg',
'.cda' : 'application/x-cda',
'.ape' : 'audio/x-ape',
'.amr' : 'audio/amr',
'.awb' : 'audio/amr-wb',
'.aac' : 'audio/aac',
'.wav' : 'audio/wav',
'.mp3' : 'audio/mpeg',
'.aff' : 'audio/aiff',
'.aif' : 'audio/aiff',
'.aiff' : 'audio/aiff',
'.flac' : 'audio/flac',
'.alac' : 'audio/alac',
'.abc' : 'text/vnd.abc',
'.tga' : 'image/x-targa',
'.webp' : 'image/webp',
'.ico' : 'image/vnd.microsoft.icon',
'.heic' : 'image/heic',
'.heif' : 'image/heif',
'.tiff' : 'image/tiff',
'.tif' : 'image/tiff',
'.jpeg' : 'image/jpeg',
'.jpg' : 'image/jpeg',
'.png' : 'image/png',
'.apng' : 'image/apng',
'.gif' : 'image/gif',
'.bmp' : 'image/bmp',
'.svg' : 'image/svg+xml',
'.psd' : 'image/vnd.adobe.photoshop',
'.gv' : 'text/vnd.graphviz',
'.dot' : 'text/vnd.graphviz',
'.xdot' : 'text/vnd.graphviz',
'.proto' : 'application/x+protobuf',
'.ps' : 'application/postscript',
'.sql' : 'application/sql',
'.swf' : 'application/x-shockwave-flash',
'.rss' : 'application/rss+xml',
'.jar' : 'application/java-archive',
'.js' : 'application/javascript',
'.css' : 'text/css',
'.htm' : 'text/html',
'.html' : 'text/html',
'.txt' : 'text/plain',
'.md' : 'text/markdown',
'.ics' : 'text/calendar',
'.rtf' : 'application/rtf',
'.pdf' : 'application/pdf',
'.epub' : 'application/epub+zip',
'.json' : 'application/json',
'.dtd' : 'application/xml-dtd',
'.xml' : 'application/xml',
'.yaml' : 'text/yaml',
'.yml' : 'text/yaml',
'.csv' : 'text/csv',
'.tsv' : 'text/tab-separated-values',
'.ttf' : 'application/x-font-ttf',
'.otf' : 'font/otf',
'.gf' : 'application/x-tex-gf',
'.avi' : 'video/x-msvideo',
'.ivf' : 'video/x-ivf',
'.flv' : 'video/flv',
'.jpgv' : 'video/jpeg',
'.mjpeg' : 'video/x-motion-jpeg',
'.mjpg' : 'video/x-motion-jpeg',
'.ogv' : 'video/ogg',
'.webm' : 'video/webm',
'.mpg' : 'video/mpeg',
'.mpeg' : 'video/mpeg',
'.mp4' : 'video/mp4',
'.m4v' : 'video/mp4',
'.m4s' : 'video/iso.segment',
'.f4v' : 'video/x-f4v',
'.f4f' : 'video/f4f',
'.f4m' : 'application/f4m+xml',
'.bik' : 'video/vnd.radgametools.bink',
'.xvid' : 'video/xvid',
'.cast' : 'application/x-asciicast',
'.qt' : 'video/quicktime',
'.mov' : 'video/quicktime',
'.mkv' : 'video/x-matroska',
'.mk3d' : 'video/x-matroska-3d',
'.mxf' : 'application/mxf',
'.gxf' : 'application/gxf',
'.ism' : 'application/vnd.ms-sstr+xml',
'.m3u8' : 'application/x-mpegURL',
'.ts' : 'video/MP2T',
'.mpd' : 'application/dash+xml',
'.har' : 'application/har+json',
'.hdf' : 'application/x-hdf',
'.rdf' : 'application/rdf+xml',
'.owl' : 'application/owl+xml',
'.ttl' : 'text/turtle',
'.nt' : 'application/n-triples',
'.n3' : 'text/n3',
'.jsonld' : 'application/ld+json',
'.mtl' : 'model/mtl',
'.obj' : 'model/obj',
'.stl' : 'model/stl',
'.usda' : 'model/vnd.usda',
'.usdz' : 'model/vnd.usdz+zip',
'.glb' : 'model/gltf-binary',
'.gltf' : 'model/gltf+json',
'.fbx' : 'application/fbx',
'.dae' : 'model/vnd.collada+xml',
'.x3d' : 'model/x3d+xml',
'.vrml' : 'model/vrml',
'.blend' : 'application/x-blender',
'.wad' : 'application/x-doom',
'.bsp' : 'application/vnd.valve.source.compiled-map',
'.mpq' : 'application/vnd.blizzard-archive-mopaq',
'.emf' : 'application/x-msmetafile',
'.emz' : 'application/x-msmetafile+zip'
}
function handler(req,resp){
var pu = url.parse(req.url);
var p = process.cwd() + pu.pathname;
console.log(pu.pathname);
try{
var s = fs.statSync(p);
}
catch(e){
console.log(e.toString());
resp.writeHead(404,{'Content-type':'text/html'});
resp.end("<HTML>Not found</HTML>");
return;
}
if( s.isDirectory() ){
var pfx = pu.pathname;
if( pfx.length === 0 )
pfx='/';
if( pfx[0] !== '/' )
pfx[0] = '/';
if( pfx[pfx.length-1] != '/' )
pfx += '/';
var L = fs.readdirSync(p);
resp.writeHead(200,{"Content-type":"text/html"});
resp.write("<!DOCTYPE html>\n");
resp.write("<html>\n");
resp.write("<head><meta charset='utf-8'></head>");
resp.write("<body><ul>");
for(var i=0;i<L.length;++i){
resp.write("<li><a href='"+pfx+L[i]+"'>"+L[i]+"</a></li>\n");
}
resp.end("</ul></body></html>");
}
else{
var rs = fs.createReadStream(p);
var i = p.lastIndexOf(".");
var suffix;
if( i === -1 )
suffix='';
else
suffix = p.substr(i);
var mimetype;
if( mimetypes[suffix] === undefined )
mimetype='application/octet-stream';
else
mimetype=mimetypes[suffix];
resp.writeHead(200,{'Content-type':mimetype});
rs.pipe(resp);
}
}
var server = http.createServer(handler);
server.listen(8081,'127.0.0.1');
console.log("Listening on port 8081");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment