proper post and put handling
This commit is contained in:
@@ -16,6 +16,7 @@ let PORT = 1280;
|
|||||||
// Load store file
|
// Load store file
|
||||||
//
|
//
|
||||||
let store = {}; // deafult empty object
|
let store = {}; // deafult empty object
|
||||||
|
let json='';
|
||||||
try {
|
try {
|
||||||
let json = fs.readFileSync(DATA_FILE);
|
let json = fs.readFileSync(DATA_FILE);
|
||||||
store = JSON.parse(json);
|
store = JSON.parse(json);
|
||||||
@@ -63,27 +64,27 @@ let serveFile = function(loc, req, resp) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let serveRestData = function(key, req, resp) {
|
let serveRestData = function(urlPath, req, resp) {
|
||||||
console.log(req.method + ' request for key [' + key + ']');
|
console.log(req.method + ' request for path [' + urlPath + ']');
|
||||||
|
|
||||||
switch (req.method) {
|
switch (req.method) {
|
||||||
|
|
||||||
case 'GET':
|
case 'GET':
|
||||||
let exactMatch=!key.endsWith('/');
|
let exactMatch=!urlPath.endsWith('/');
|
||||||
let matchFound=false;
|
let matchFound=false;
|
||||||
|
|
||||||
if (exactMatch) {
|
if (exactMatch) { // assumes that only a single resource has this ID.
|
||||||
console.log(" - Exact Match.");
|
console.log(" - Exact Match.");
|
||||||
if (store.hasOwnProperty(key)) {
|
if (store.hasOwnProperty(urlPath)) {
|
||||||
resp.writeHead(200, { 'Content-Type' : 'application/json' });
|
resp.writeHead(200, { 'Content-Type' : 'application/json' });
|
||||||
resp.write(JSON.stringify(store[key]) + '\n');
|
resp.write(JSON.stringify(store[urlPath]) + '\n');
|
||||||
matchFound=true;
|
matchFound=true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
console.log(" - All Matches.");
|
console.log(" - All Matches.");
|
||||||
let matches=[];
|
let matches=[];
|
||||||
Object.keys(store).forEach(theKey=>{
|
Object.keys(store).forEach(theKey=>{
|
||||||
if (theKey.startsWith(key)){
|
if (theKey.startsWith(urlPath)){
|
||||||
matches.push(store[theKey]);
|
matches.push(store[theKey]);
|
||||||
matchFound=true;
|
matchFound=true;
|
||||||
}
|
}
|
||||||
@@ -100,19 +101,18 @@ let serveRestData = function(key, req, resp) {
|
|||||||
resp.writeHead(404, { 'Content-Type' : 'application/json' });
|
resp.writeHead(404, { 'Content-Type' : 'application/json' });
|
||||||
resp.write('{"success":false, "error":"Not Found"}');
|
resp.write('{"success":false, "error":"Not Found"}');
|
||||||
} else {
|
} else {
|
||||||
console.log(" - Match not found.");
|
console.log(" - Match found.");
|
||||||
}
|
}
|
||||||
resp.end();
|
resp.end();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'PUT':
|
case 'PUT':
|
||||||
case 'POST':
|
json = '';
|
||||||
let json = '';
|
|
||||||
req.on('data', function(chunk) { json += chunk; });
|
req.on('data', function(chunk) { json += chunk; });
|
||||||
req.on('end', function() {
|
req.on('end', function() {
|
||||||
try {
|
try {
|
||||||
let data = JSON.parse(json);
|
let data = JSON.parse(json);
|
||||||
store[key] = data;
|
store[urlPath] = data;
|
||||||
resp.writeHead(201, { 'Content-Type' : 'application/json' });
|
resp.writeHead(201, { 'Content-Type' : 'application/json' });
|
||||||
resp.write('{"success":true}');
|
resp.write('{"success":true}');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -123,14 +123,68 @@ let serveRestData = function(key, req, resp) {
|
|||||||
flush();
|
flush();
|
||||||
});
|
});
|
||||||
break;
|
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':
|
case 'OPTIONS':
|
||||||
resp.end();
|
resp.end();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'DELETE':
|
case 'DELETE':
|
||||||
if (store.hasOwnProperty(key)) {
|
if (store.hasOwnProperty(urlPath)) {
|
||||||
delete store[key];
|
delete store[urlPath];
|
||||||
resp.writeHead(204, { 'Content-Type' : 'application/json' });
|
resp.writeHead(204, { 'Content-Type' : 'application/json' });
|
||||||
resp.write('{"success":true}');
|
resp.write('{"success":true}');
|
||||||
} else {
|
} else {
|
||||||
@@ -158,7 +212,7 @@ http.createServer(function(req, resp) {
|
|||||||
resp.setHeader('Access-Control-Allow-Headers', 'X-Request-With, content-type');
|
resp.setHeader('Access-Control-Allow-Headers', 'X-Request-With, content-type');
|
||||||
resp.setHeader('Vary', 'Origin');
|
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 + ']');
|
console.log('Serving [' + req.url + ']');
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user