Created
October 25, 2012 09:09
-
-
Save geta6/3951548 to your computer and use it in GitHub Desktop.
simplest HTTP test server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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