Skip to content

Instantly share code, notes, and snippets.

@geta6
Created October 25, 2012 09:09
Show Gist options
  • Save geta6/3951548 to your computer and use it in GitHub Desktop.
Save geta6/3951548 to your computer and use it in GitHub Desktop.
simplest HTTP test server
var http = require('http'), fs = require('fs'), url = require('url');
var server = {
root: '.',
port: 8080,
index: 'index.html'
};
http.createServer(function (req, res) {
var location = url.parse(req.url);
var path = server.root + '/' + server.index;
if (location.pathname !== '/') {
path = server.root + location.pathname;
if (location.pathname.substr(-1) === '/')
path += server.index;
}
fs.exists(path, function(exists) {
if (exists) {
console.log(req.method, '\033[32m200\033[39m', path);
res.writeHead(200);
res.end(fs.readFileSync(path));
} else {
console.log(req.method, '\033[31m404\033[39m', path);
res.writeHead(404);
res.end('Document Not Found.');
}
});
}).listen(server.port);
console.log('Running at', server.port, '\n');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment