dat.gui/server/server.js

35 lines
1.0 KiB
JavaScript
Raw Normal View History

2014-09-15 22:59:28 +00:00
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
var filename = /\/([^/]+\.json)$/.exec(req.url);
if (!filename) {
filename = 'dat-gui.json';
} else {
filename = filename[1];
}
switch(req.method) {
case 'GET':
if (!fs.existsSync(filename)) {
res.writeHead(404);
res.end();
return
}
2014-09-16 03:44:07 +00:00
res.writeHead(200, {'Content-Type': 'application/json', 'Access-Control-Allow-Origin': 'http://localhost'});
2014-09-15 22:59:28 +00:00
res.end(fs.readFileSync(filename, 'utf8'));
break;
case 'POST':
var data = '';
req.on('data', function(d) {data += d;})
req.on('end', function() {
2014-09-16 03:44:07 +00:00
res.writeHead(200, {'Access-Control-Allow-Origin': 'http://localhost'});
2014-09-15 22:59:28 +00:00
fs.writeFileSync(filename, data);
res.end();
})
break;
default:
res.writeHead(404);
res.end();
}
2014-09-16 03:44:07 +00:00
}).listen(7999);