From a7753cda95ccc7a7e8260951894c6b4a8a665928 Mon Sep 17 00:00:00 2001 From: Doug Fritz Date: Mon, 15 Sep 2014 15:59:28 -0700 Subject: [PATCH] simple server starter code --- server/server.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 server/server.js diff --git a/server/server.js b/server/server.js new file mode 100644 index 0000000..a02bfc8 --- /dev/null +++ b/server/server.js @@ -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);