Skip to content

Instantly share code, notes, and snippets.

@Gomah
Created November 8, 2013 01:43
Show Gist options
  • Save Gomah/7364945 to your computer and use it in GitHub Desktop.
Save Gomah/7364945 to your computer and use it in GitHub Desktop.
Static webserver @node.js
// Variables dc (http://nodejs.org/api/)
var http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs"),
port = process.argv[2] || 1337, // Array ex -> http://nodejs.org/docs/latest/api/process.html#process_process_argv
mimeTypes = {
// Basic mimes
'asc' : 'text/plain',
'au' : 'audio/basic',
'rtf' : 'text/rtf',
'txt' : 'text/plain',
'xsl' : 'text/xml'
'wml' : 'text/vnd.wap.wml',
'wmls' : 'text/vnd.wap.wmlscript',
// Web mimes
'css' : 'text/css',
'htm' : 'text/html',
'html' : 'text/html',
'js' : 'application/x-javascript',
'json' : 'application/json',
'csv' : 'application/vnd.ms-excel',
'atom' : 'application/atom+xml',
'rss' : 'application/rss+xml',
'xhtml' : 'application/xhtml+xml',
'xml' : 'text/xml',
// Images &mimes
'bmp' : 'image/bmp',
'gif' : 'image/gif',
'jpeg' : 'image/jpeg',
'jpg' : 'image/jpeg',
'jpe' : 'image/jpeg',
'png' : 'image/png',
'ico' : 'image/vnd.microsoft.icon',
'tiff' : 'image/tiff',
'tif' : 'image/tiff',
'svg' : 'image/svg+xml',
// Videos mimes
'avi' : 'video/x-msvideo',
'flv' : 'video/x-flv',
'mpeg' : 'video/mpeg',
'mpg' : 'video/mpeg',
'mpe' : 'video/mpeg',
'qt' : 'video/quicktime',
'mov' : 'video/quicktime',
'wmv' : 'video/x-ms-wmv',
// Audio mimes
'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',
'snd' : 'audio/basic',
'midi' : 'audio/midi',
'mid' : 'audio/midi',
'm3u' : 'audio/x-mpegurl',
// Application mimes
// Shell
'dll' : 'application/octet-stream',
'bin' : 'application/octet-stream',
'class' : 'application/octet-stream',
'exe' : 'application/octet-stream',
// Based app
'doc' : 'application/msword',
'dvi' : 'application/x-dvi',
'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'
};
// Create the static server (http://nodejs.org/api/http.html#http_http_createserver_requestlistener)
// For https, take a look to this page : http://nodejs.org/api/https.html#https_class_https_server
http.createServer(function(request, response) {
var stserver = url.parse(request.url).pathname, filename = path.join(process.cwd(), stserver);
// Check for the path (http://nodejs.org/api/fs.html#fs_fs_exists_path_callback)
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';
// Start reading & writing
// http://nodejs.org/api/fs.html#fs_fs_readfile_filename_options_callback
// http://nodejs.org/api/http.html#http_response_writehead_statuscode_reasonphrase_headers
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("Server running at : http://localhost:" + port + "/\nCTRL + C to stop.");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment