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