public
Last active — forked from iamnoah/index.js

Simple node.js webserver with logging. Serves whatever files are reachable from the directory where node is running. [support for Node for Windows]

  • Download Gist
index.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
/*
* Fork & Refactor of https://gist.github.com/246761
* -> Credit: Noah Sloan <http://noahsloan.com>
*/
 
/**
* Simple webserver with logging. Serves whatever files are reachable from
* the directory where node is running. Supports Windows port of node.
*/
var fs = require('fs'),
path = require('path'),
sys = require('util');
 
var DEBUG = 0, INFO = 1, WARN = 2, ERROR = 3;
var LOG_LEVEL = DEBUG;
 
var PORT = 8080;
 
require("http").createServer(function(req,resp) {
// don't allow ../ in paths
var file = '.' + req.url;
if (file.substr(-1) === '/') file += 'index.html';
var ext = path.extname(file);
var contentType = 'text/html';
log(ERROR,ext);
switch(ext) {
case '.htm':
contentType = 'text/html';
break;
case '.html':
contentType = 'text/html';
break;
case '.js':
contentType = 'text/javascript';
break;
case '.css':
contentType = 'text/css';
break;
case '.png':
contentType = 'image/png';
break;
case '.jpeg':
contentType = 'image/jpeg';
break;
case '.jpg':
contentType = 'image/jpeg';
break;
case '.gif':
contentType = 'image/gif';
break;
case '.ico':
contentType = 'image/gif';
break;
default:
contentType = 'text/plain';
break;
}
log(DEBUG,"Got request for",file,contentType);
streamFile(file,resp,contentType);
}).listen(PORT);
 
log(INFO,"Server running on port",PORT);
 
function streamFile(file,resp,contentType) {
path.exists(file, function(exists) {
if (exists) {
fs.readFile(file, function(err,data) {
if (err) {
resp.writeHead(500);
resp.end();
log(WARN, "No such file: ", file);
} else {
resp.writeHead(200, { 'Content-Type': contentType });
resp.end(data, 'utf-8');
}
});
} else {
resp.writeHead(404);
resp.end();
}
});
}
 
/* Logging/Utility Functions */
function log(level) {
if(level >= LOG_LEVEL) sys.puts(join(slice(arguments,1)));
}
function slice(array,start) {
return Array.prototype.slice.call(array,start);
}
function isString(s) {
return typeof s === "string" || s instanceof String;
}
function flatten(array) {
var result = [], i, len = array && array.length;
if(len && !isString(array)) {
for(i = 0; i < len; i++) {
result = result.concat(flatten(array[i]));
}
} else if(len !== 0) {
result.push(array);
}
return result;
}
function join() {
return flatten(slice(arguments,0)).join(" ");
}

So the nice thing about python SimpleHTTPServer is that you can run it from any directory like so python -m SimpleHTTPServer. Wouldn't it be cool to include a script to run this from any directory? Even better, this supports any request method whereas SimpleHTTPServer supports only GET.

Here's a script to do that

(node /path/to/index.js &)

I always run mine from the home directory, so I made it force to home directory

(cd $HOME && node $HOME/index.js &)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.