- handle return all

- handle put and post same
- send feedback as JSON
- add CORS headers
This commit is contained in:
Joe Tretter
2020-08-28 23:58:45 -05:00
parent 99ad597bca
commit b11e5144e3

View File

@@ -1,25 +1,23 @@
// //
// Dependencies // Dependencies
// //
var http = require('http'); let http = require('http');
var fs = require('fs'); let fs = require('fs');
// //
// Constants // Constants
// //
var DATA_LOCATION = 'data'; // both the url root and the location of data file let DATA_LOCATION = 'data'; // both the url root and the location of data file
var DATA_LOCATION_LEN = DATA_LOCATION.length; let DATA_LOCATION_LEN = DATA_LOCATION.length;
var DATA_FILE = __dirname + '/' + DATA_LOCATION; let DATA_FILE = __dirname + '/' + DATA_LOCATION;
var PORT = 80; let PORT = 1280;
// //
// Load store file // Load store file
// //
var store = {}; // deafult empty object let store = {}; // deafult empty object
try { try {
var json = fs.readFileSync(DATA_FILE); let json = fs.readFileSync(DATA_FILE);
store = JSON.parse(json); store = JSON.parse(json);
console.log('Read store file'); console.log('Read store file');
} catch (e) { // lazy error handling } catch (e) { // lazy error handling
@@ -30,14 +28,14 @@ try {
// //
// Flush to store file on exit // Flush to store file on exit
// //
var flush = function() let flush = function()
{ {
console.log('Caught deadly signal.'); console.log('Caught deadly signal.');
console.log('Flushing data to file [' + DATA_FILE + ']\n'); console.log('Flushing data to file [' + DATA_FILE + ']\n');
fs.writeFileSync(DATA_FILE, JSON.stringify(store)); fs.writeFileSync(DATA_FILE, JSON.stringify(store));
}; };
var handleInterrupt = function() { flush(); process.exit(); }; let handleInterrupt = function() { flush(); process.exit(); };
process.on('SIGINT', handleInterrupt); process.on('SIGINT', handleInterrupt);
process.on('SIGTERM', handleInterrupt); process.on('SIGTERM', handleInterrupt);
@@ -48,9 +46,8 @@ process.on('SIGHUP', flush); // HUP flushes to file but keeps the server runnin
// If request URI is prefixed with the value of DATA_LOCATION, we treat incoming request as a REST request // If request URI is prefixed with the value of DATA_LOCATION, we treat incoming request as a REST request
// Otherwise we serve the file referenced by the URI // Otherwise we serve the file referenced by the URI
// //
var serveFile = function(loc, req, resp) let serveFile = function(loc, req, resp) {
{ let fullPath = __dirname + '/' + loc;
var fullPath = __dirname + '/' + loc;
console.log('Trying to serve file at [' + fullPath + ']'); console.log('Trying to serve file at [' + fullPath + ']');
if (req.method !== 'GET') { // read only! if (req.method !== 'GET') { // read only!
@@ -67,34 +64,61 @@ var serveFile = function(loc, req, resp)
} }
}; };
var serveRestData = function(key, req, resp) let serveRestData = function(key, req, resp) {
{
console.log(req.method + ' request for key [' + key + ']'); console.log(req.method + ' request for key [' + key + ']');
switch (req.method) { switch (req.method) {
case 'GET': case 'GET':
if (store.hasOwnProperty(key)) { let exactMatch=!key.endsWith('/');
resp.writeHead(200, { 'Content-Type' : 'application/json' }); let matchFound=false;
resp.write(JSON.stringify(store[key]) + '\n');
if (exactMatch) {
console.log(" - Exact Match.");
if (store.hasOwnProperty(key)) {
resp.writeHead(200, { 'Content-Type' : 'application/json' });
resp.write(JSON.stringify(store[key]) + '\n');
matchFound=true;
}
} else {
console.log(" - All Matches.");
let matches=[];
Object.keys(store).forEach(theKey=>{
if (theKey.startsWith(key)){
matches.push(store[theKey]);
matchFound=true;
}
});
if (matchFound) {
resp.writeHead(200, { 'Content-Type' : 'application/json' });
resp.write(JSON.stringify(matches));
}
}
if (!matchFound) {
console.log(" - Match NOT found.");
resp.writeHead(404, { 'Content-Type' : 'application/json' });
resp.write('{"success":false, "error":"Not Found"}');
} else { } else {
resp.writeHead(404); console.log(" - Match not found.");
resp.write('Not Found\n'); }
}
resp.end(); resp.end();
break; break;
case 'PUT': case 'PUT':
var json = ''; case 'POST':
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 {
var data = JSON.parse(json); let data = JSON.parse(json);
store[key] = data; store[key] = data;
resp.writeHead(204); resp.writeHead(201, { 'Content-Type' : 'application/json' });
resp.write('{"success":true}');
} catch (e) { } catch (e) {
resp.writeHead(400); resp.writeHead(400, { 'Content-Type' : 'application/json' });
resp.write('Invalid JSON\n'); resp.write('{"success":false, "error":"Invalid JSON"}');
} }
resp.end(); resp.end();
}); });
@@ -103,16 +127,19 @@ var serveRestData = function(key, req, resp)
case 'DELETE': case 'DELETE':
if (store.hasOwnProperty(key)) { if (store.hasOwnProperty(key)) {
delete store[key]; delete store[key];
resp.writeHead(204); resp.writeHead(204, { 'Content-Type' : 'application/json' });
resp.write('{"success":true}');
} else { } else {
resp.writeHead(404); resp.writeHead(404, { 'Content-Type' : 'application/json' });
resp.write('{"success":false, "error":"Not Found"}');
} }
resp.end(); resp.end();
flush();
break; break;
default: default:
resp.writeHead(405); resp.writeHead(405, { 'Content-Type' : 'application/json' });
resp.write('Unsupported method\n'); resp.write('{"success":false, "error":"Unsupported method"}');
resp.end(); resp.end();
} }
@@ -122,15 +149,18 @@ var serveRestData = function(key, req, resp)
// Listen for connections // Listen for connections
// //
http.createServer(function(req, resp) { http.createServer(function(req, resp) {
// CORS headers
console.log('Serving [' + req.url + ']'); resp.setHeader('Access-Control-Allow-Origin', '*');
var uri = req.url.replace(/^\/+|\/+$/g, ''); // trim slashes resp.setHeader('Vary', 'Origin');
if (uri.substr(0, DATA_LOCATION_LEN + 1) === DATA_LOCATION + '/') { // rest request let uri = req.url //.replace(/^\/+|\/+$/g, ''); // trim slashes
serveRestData(uri.substr(DATA_LOCATION_LEN + 1), req, resp); console.log('Serving [' + req.url + ']');
} else { // try to serve the file
if (uri === '') { uri = 'index.html'; } if (uri === '/') { // server index file
uri = 'index.html';
serveFile(uri, req, resp); serveFile(uri, req, resp);
} else { // rest request
serveRestData(req.url, req, resp);
} }
}).listen(PORT); }).listen(PORT);