-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
49 lines (40 loc) · 1.44 KB
/
index.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
var http = require('http'),
url = require('url'),
path = require('path'),
fs = require('fs');
var mimeTypes = {
"html": "text/html",
"js": "text/javascript",
"css": "text/css",
"json": "application/json"
};
http.createServer(function (req, res) {
var uri = decodeURI(url.parse(req.url).pathname);
if (uri === "/reports/ProtectedDemo.json" && req.headers["x-auth-token"] !== "*YOUR TOKEN*") {
console.log("No custom header provided");
res.writeHead(403, "Forbidden", { 'Content-Type': 'text/plain' });
res.end();
return;
}
var filename = path.join(process.cwd(), uri);
try {
if (fs.statSync(filename).isDirectory()) filename += '/index.html';
fs.exists(filename, function (exists) {
if (!exists) {
console.log("not exists: " + filename);
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('404 Not Found\n');
res.end();
return;
}
var mimeType = mimeTypes[path.extname(filename).split(".")[1]];
if (mimeType) res.writeHead(200, { 'Content-Type': mimeType });
var fileStream = fs.createReadStream(filename);
fileStream.pipe(res);
});
}
catch (e) {
return;
}
}).listen(8889);
console.log("Static file server running at http://localhost:" + 8889 + "/\nCTRL + C to shutdown");