static.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/env node
  2. var http = require("http")
  3. , path = require("path")
  4. , mime = require("mime")
  5. , url = require("url")
  6. , fs = require("fs")
  7. , port = process.env.PORT || 8888
  8. , ip = process.env.IP || "0.0.0.0";
  9. // compatibility with node 0.6
  10. if (!fs.exists)
  11. fs.exists = path.exists;
  12. var allowSave = process.argv.indexOf("--allow-save") != -1;
  13. http.createServer(function(req, res) {
  14. var uri = unescape(url.parse(req.url).pathname)
  15. , filename = path.join(process.cwd(), uri);
  16. if (req.method == "PUT") {
  17. if (!allowSave)
  18. return error(res, 404, "Saving not allowed pass --allow-save to enable");
  19. return save(req, res, filename);
  20. }
  21. fs.exists(filename, function(exists) {
  22. if (!exists)
  23. return error(res, 404, "404 Not Found\n" + filename);
  24. if (fs.statSync(filename).isDirectory()) {
  25. var files = fs.readdirSync(filename);
  26. res.writeHead(200, {"Content-Type": "text/html"});
  27. files.push(".", "..");
  28. var html = files.map(function(name) {
  29. var href = uri + "/" + name;
  30. href = href.replace(/[\/\\]+/g, "/").replace(/\/$/g, "");
  31. try {
  32. var stat = fs.statSync(filename + "/" + name);
  33. if (stat.isDirectory())
  34. href += "/";
  35. return "<a href='" + href + "'>" + name + "</a><br>";
  36. } catch(e) {}
  37. }).filter(Boolean);
  38. res._hasBody && res.write(html.join(""));
  39. res.end();
  40. return;
  41. }
  42. fs.readFile(filename, "binary", function(err, file) {
  43. if (err) {
  44. res.writeHead(500, { "Content-Type": "text/plain" });
  45. res.write(err + "\n");
  46. res.end();
  47. return;
  48. }
  49. var contentType = mime.lookup(filename) || "text/plain";
  50. res.writeHead(200, { "Content-Type": contentType });
  51. res.write(file, "binary");
  52. res.end();
  53. });
  54. });
  55. }).listen(port, ip);
  56. function error(res, status, message, error) {
  57. console.error(error || message);
  58. res.writeHead(status, { "Content-Type": "text/plain" });
  59. res.write(message);
  60. res.end();
  61. }
  62. function save(req, res, filePath) {
  63. var data = "";
  64. req.on("data", function(chunk) {
  65. data += chunk;
  66. });
  67. req.on("error", function() {
  68. error(res, 404, "Could't save file");
  69. });
  70. req.on("end", function() {
  71. try {
  72. fs.writeFileSync(filePath, data);
  73. }
  74. catch (e) {
  75. return error(res, 404, "Could't save file", e);
  76. }
  77. res.statusCode = 200;
  78. res.end("OK");
  79. console.log("saved ", filePath);
  80. });
  81. }
  82. function getLocalIps() {
  83. var os = require("os");
  84. var interfaces = os.networkInterfaces ? os.networkInterfaces() : {};
  85. var addresses = [];
  86. for (var k in interfaces) {
  87. for (var k2 in interfaces[k]) {
  88. var address = interfaces[k][k2];
  89. if (address.family === "IPv4" && !address.internal) {
  90. addresses.push(address.address);
  91. }
  92. }
  93. }
  94. return addresses;
  95. }
  96. console.log("http://" + (ip == "0.0.0.0" ? getLocalIps()[0] : ip) + ":" + port);