Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Simple node.js webserver with logging. Serves whatever files are reachable from the directory where node is running. [support for Node for Windows]
/*
* 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(" ");
}
@drewwells

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 &)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment