2011-02-15 19:50:15 +08:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
2012-07-10 10:06:56 +04:00
|
|
|
var http = require("http")
|
|
|
|
|
, path = require("path")
|
|
|
|
|
, mime = require("mime")
|
|
|
|
|
, url = require("url")
|
|
|
|
|
, fs = require("fs")
|
2012-11-09 18:02:13 -05:00
|
|
|
, port = process.env.PORT || 8888
|
|
|
|
|
, ip = process.env.IP || "0.0.0.0";
|
2012-07-10 10:06:56 +04:00
|
|
|
|
|
|
|
|
// compatibility with node 0.6
|
|
|
|
|
if (!fs.exists)
|
|
|
|
|
fs.exists = path.exists;
|
2011-02-11 07:56:31 +01:00
|
|
|
|
|
|
|
|
http.createServer(function(request, response) {
|
|
|
|
|
|
|
|
|
|
var uri = url.parse(request.url).pathname
|
|
|
|
|
, filename = path.join(process.cwd(), uri);
|
|
|
|
|
|
2012-07-10 10:06:56 +04:00
|
|
|
fs.exists(filename, function(exists) {
|
2011-02-11 07:56:31 +01:00
|
|
|
if(!exists) {
|
|
|
|
|
response.writeHead(404, {"Content-Type": "text/plain"});
|
|
|
|
|
response.write("404 Not Found\n");
|
|
|
|
|
response.end();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-11 12:52:33 +01:00
|
|
|
if (fs.statSync(filename).isDirectory()) filename += '/index.html';
|
2011-02-11 07:56:31 +01:00
|
|
|
|
|
|
|
|
fs.readFile(filename, "binary", function(err, file) {
|
|
|
|
|
if(err) {
|
|
|
|
|
response.writeHead(500, {"Content-Type": "text/plain"});
|
|
|
|
|
response.write(err + "\n");
|
|
|
|
|
response.end();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-15 20:09:19 +08:00
|
|
|
var contentType = mime.lookup(filename) || "text/plain";
|
|
|
|
|
response.writeHead(200, {"Content-Type": contentType});
|
2011-02-11 07:56:31 +01:00
|
|
|
response.write(file, "binary");
|
|
|
|
|
response.end();
|
|
|
|
|
});
|
|
|
|
|
});
|
2012-11-09 18:02:13 -05:00
|
|
|
}).listen(port, ip);
|
2011-07-19 13:17:21 +02:00
|
|
|
|
|
|
|
|
console.log("http://localhost:" + port);
|