forked from ddprrt/fh-offlineapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.js
56 lines (53 loc) · 2.28 KB
/
read.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
var fs = require('fs');
var http = require('http');
http.createServer(function (req, res) {
// set up some routes
switch(req.url) {
case '/api':
if (req.method == 'GET') {
console.log("[200] " + req.method + " to " + req.url);
fs.readFile('./demo.json', function(err, json) {
if(err) {
res.writeHead(500, "Not found", {'Content-Type': 'text/html'});
res.end('<html><head><title>Errör</title></head><body><h1>Bedoooow</h1></body></html>');
} else {
console.log(json.toString());
res.writeHead(200, "OK", {'Content-Type': 'text/json'});
res.write(json.toString(), 'utf-8');
res.end();
}
});
} else if (req.method == 'POST') {
var dt;
console.log("[200] " + req.method + " to " + req.url);
req.on('data', function(chunk) {
console.log("Received body data:");
console.log(chunk.toString());
dt = chunk.toString();
var stream = fs.createWriteStream('demo.json');
stream.once('open', function(fd) {
stream.write(chunk.toString());
stream.end();
});
});
req.on('end', function() {
res.writeHead(200, "OK", {'Content-Type': 'text/json'});
res.write(dt, 'utf-8');
res.end();
});
}
break;
default:
fs.readFile('.' + req.url, function (err, html) {
if (err) {
res.writeHead(404, "Not found", {'Content-Type': 'text/html'});
res.end('<html><head><title>404 - Not found</title></head><body><h1>Not found.</h1></body></html>');
console.log("[404] " + req.method + " to " + req.url);
} else {
res.writeHead(200, "OK", {'Content-Type': 'text/html'});
res.write(html);
res.end();
}
});
};
}).listen(8080);