static.js 3.3 KB

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