From e359485d9476c0609616b8c345ee7de269b619b9 Mon Sep 17 00:00:00 2001 From: Joe Tretter Date: Sat, 29 Aug 2020 23:12:27 -0500 Subject: [PATCH] proper post and put handling --- dumbserver.js | 82 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 68 insertions(+), 14 deletions(-) diff --git a/dumbserver.js b/dumbserver.js index ef6f610..2f1c8e8 100644 --- a/dumbserver.js +++ b/dumbserver.js @@ -16,6 +16,7 @@ let PORT = 1280; // Load store file // let store = {}; // deafult empty object +let json=''; try { let json = fs.readFileSync(DATA_FILE); store = JSON.parse(json); @@ -63,27 +64,27 @@ let serveFile = function(loc, req, resp) { } }; -let serveRestData = function(key, req, resp) { - console.log(req.method + ' request for key [' + key + ']'); +let serveRestData = function(urlPath, req, resp) { + console.log(req.method + ' request for path [' + urlPath + ']'); switch (req.method) { case 'GET': - let exactMatch=!key.endsWith('/'); + let exactMatch=!urlPath.endsWith('/'); let matchFound=false; - if (exactMatch) { + if (exactMatch) { // assumes that only a single resource has this ID. console.log(" - Exact Match."); - if (store.hasOwnProperty(key)) { + if (store.hasOwnProperty(urlPath)) { resp.writeHead(200, { 'Content-Type' : 'application/json' }); - resp.write(JSON.stringify(store[key]) + '\n'); + resp.write(JSON.stringify(store[urlPath]) + '\n'); matchFound=true; } } else { console.log(" - All Matches."); let matches=[]; Object.keys(store).forEach(theKey=>{ - if (theKey.startsWith(key)){ + if (theKey.startsWith(urlPath)){ matches.push(store[theKey]); matchFound=true; } @@ -100,19 +101,18 @@ let serveRestData = function(key, req, resp) { resp.writeHead(404, { 'Content-Type' : 'application/json' }); resp.write('{"success":false, "error":"Not Found"}'); } else { - console.log(" - Match not found."); + console.log(" - Match found."); } resp.end(); break; case 'PUT': - case 'POST': - let json = ''; + json = ''; req.on('data', function(chunk) { json += chunk; }); req.on('end', function() { try { let data = JSON.parse(json); - store[key] = data; + store[urlPath] = data; resp.writeHead(201, { 'Content-Type' : 'application/json' }); resp.write('{"success":true}'); } catch (e) { @@ -123,14 +123,68 @@ let serveRestData = function(key, req, resp) { flush(); }); break; + + + case 'POST': + json = ''; + req.on('data', function(chunk) { json += chunk; }); + req.on('end', function() { + try { + let data = JSON.parse(json); + if (data.ID === undefined) { + resp.writeHead(400, { 'Content-Type' : 'application/json' }); + resp.write('{"success":false, "error":"The ID Field needs to be given"}'); + } else { + // make sure the ID from the data is used as the final resource Identifier, + // even if the URL has none or says different. + let re=new RegExp("/[^/]*$"); + urlPath=urlPath.replace(re,""); + urlPath=urlPath+"/"+data.ID; + console.log(" - storing with resource key: [" + urlPath + "]"); + + store[urlPath] = data; + + resp.writeHead(201, { 'Content-Type' : 'application/json' }); + resp.write(JSON.stringify(store[urlPath])); + } + } catch (e) { + resp.writeHead(400, { 'Content-Type' : 'application/json' }); + resp.write('{"success":false, "error":"Invalid JSON"}'); + } + resp.end(); + flush(); + }); + break; + + case 'PATCH': + json = ''; + req.on('data', function(chunk) { json += chunk; }); + req.on('end', function() { + try { + let data = JSON.parse(json); + Object.keys(data).forEach(theKey=>{ + store[urlPath][theKey] = data[theKey] + }); + + resp.writeHead(201, { 'Content-Type' : 'application/json' }); + resp.write('{"success":true}'); + } catch (e) { + resp.writeHead(400, { 'Content-Type' : 'application/json' }); + resp.write('{"success":false, "error":"Invalid JSON"}'); + } + resp.end(); + flush(); + }); + break; + case 'OPTIONS': resp.end(); break; case 'DELETE': - if (store.hasOwnProperty(key)) { - delete store[key]; + if (store.hasOwnProperty(urlPath)) { + delete store[urlPath]; resp.writeHead(204, { 'Content-Type' : 'application/json' }); resp.write('{"success":true}'); } else { @@ -158,7 +212,7 @@ http.createServer(function(req, resp) { resp.setHeader('Access-Control-Allow-Headers', 'X-Request-With, content-type'); resp.setHeader('Vary', 'Origin'); - resp.setHeader('Access-Control-Allow-Methods','GET, POST, PUT, DELETE, OPTIONS'); + resp.setHeader('Access-Control-Allow-Methods','GET, PATCH, POST, PUT, DELETE, OPTIONS'); console.log('Serving [' + req.url + ']');