mirror of
https://github.com/dataarts/dat.gui.git
synced 2024-12-12 04:08:27 +00:00
simple server starter code
This commit is contained in:
parent
d1915de735
commit
a7753cda95
34
server/server.js
Normal file
34
server/server.js
Normal file
@ -0,0 +1,34 @@
|
||||
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
|
||||
}
|
||||
res.writeHead(200, {'Content-Type': 'application/json'});
|
||||
res.end(fs.readFileSync(filename, 'utf8'));
|
||||
break;
|
||||
case 'POST':
|
||||
var data = '';
|
||||
req.on('data', function(d) {data += d;})
|
||||
req.on('end', function() {
|
||||
fs.writeFileSync(filename, data);
|
||||
res.writeHead(200);
|
||||
res.end();
|
||||
})
|
||||
break;
|
||||
default:
|
||||
res.writeHead(404);
|
||||
res.end();
|
||||
}
|
||||
}).listen(8080);
|
Loading…
Reference in New Issue
Block a user