-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
59 lines (51 loc) · 1.34 KB
/
app.js
File metadata and controls
59 lines (51 loc) · 1.34 KB
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
var util = require("util")
http = require("http"),
fs = require("fs"),
mime = require("mime");
base_dir = process.argv[2];
if (typeof(base_dir) === 'undefined') base_dir = process.cwd();
try {
fs.statSync(base_dir).isDirectory()
} catch (err) {
console.error('The directory you want to serve files from does not exist.');
return false;
}
util.log('Serving up static files from ' + base_dir);
function onRequest(request, response) {
if(request.method === "GET") {
var path = base_dir+request.url;
if (request.url.indexOf('favicon.ico') != -1) {
// Short-circuit favicon requests
// https://gist.github.com/763822
response.writeHead(200, {'Content-Type': 'image/x-icon'});
response.end();
return;
}
fs.readFile(path, "utf-8", function(err, html) {
if (err){
util.error('Error trying to read ' + path);
util.error(err);
throw err;
}
response.writeHeader(200,
{"Content-Type": mime.lookup(path)}
);
response.write(html);
response.end();
});
} else if(request.method === "POST") {
var data = "";
request.on("data", function(chunk) {
data += chunk;
});
request.on("end", function() {
util.log("raw: " + data);
response.writeHeader(200,
{"Content-Type": "text/html"}
);
response.write('Uploaded file.');
response.end();
});
}
}
http.createServer(onRequest).listen(8000);