From 4d28d390c3c5e9ebb65b524c45f5a03c74c66a7c Mon Sep 17 00:00:00 2001 From: Jacob van Steyn Date: Thu, 8 Sep 2022 09:54:47 -0400 Subject: [PATCH 1/3] log received data --- public/index.html | 9 +++++++-- server.improved.js | 17 +++++++---------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/public/index.html b/public/index.html index c56d620..fdd01bf 100644 --- a/public/index.html +++ b/public/index.html @@ -9,6 +9,11 @@ + + + Name + +
+ diff --git a/server.improved.js b/server.improved.js index 8fe4ba6..84ab7d9 100644 --- a/server.improved.js +++ b/server.improved.js @@ -1,69 +1,95 @@ const http = require( 'http' ), - fs = require( 'fs' ), - // IMPORTANT: you must run `npm install` in the directory for this assignment - // to install the mime library used in the following line of code - mime = require( 'mime' ), - dir = 'public/', - port = 3000 + fs = require( 'fs' ), + // IMPORTANT: you must run `npm install` in the directory for this assignment + // to install the mime library used in the following line of code + mime = require( 'mime' ), + dir = 'public/', + port = 3000 const appdata = [] const server = http.createServer( function( request,response ) { if( request.method === 'GET' ) { - handleGet( request, response ) + handleGet( request, response ) }else if( request.method === 'POST' ){ - handlePost( request, response ) + handlePost( request, response ) } }) const handleGet = function( request, response ) { - const filename = dir + request.url.slice( 1 ) + const filename = dir + request.url.slice( 1 ) if( request.url === '/' ) { sendFile( response, 'public/index.html' ) - }else{ + } else if (request.url === '/getdata'){ + response.writeHead(200, "OK", { "Content-Type": "text/plain" }); + response.write(JSON.stringify(appdata)); + response.end(); + } + else { sendFile( response, filename ) } } const handlePost = function( request, response ) { - let dataString = '' + console.log(request.url) + if (request.url === '/submit/'){ + let dataString = '' - request.on( 'data', function( data ) { - dataString += data - }) + request.on( 'data', function( data ) { + dataString += data + }) + + request.on( 'end', function() { + var data = JSON.parse( dataString ); + console.log(data); + appdata.push(data) + + response.writeHead(200, "OK", {'Content-Type': 'text/plain' }) + response.write(JSON.stringify(appdata)) + response.end() + }) + } + else if (request.url === '/delete/'){ + let dataString = '' + + request.on( 'data', function( data ) { + dataString += data + }) + + request.on( 'end', function() { + var data = JSON.parse( dataString ); + console.log(data); + delete appdata[data.id] + + response.writeHead(200, "OK", {'Content-Type': 'text/plain' }) + response.write(JSON.stringify(appdata)) + response.end() + }) + } - request.on( 'end', function() { - var data = JSON.parse( dataString ); - console.log("Received Data: " + data); - appdata.push(data) - - response.writeHead(200, "OK", {'Content-Type': 'text/plain' }) - response.json(appdata) - response.end() - }) } const sendFile = function( response, filename ) { - const type = mime.getType( filename ) + const type = mime.getType( filename ) - fs.readFile( filename, function( err, content ) { + fs.readFile( filename, function( err, content ) { - // if the error = null, then we've loaded the file successfully - if( err === null ) { + // if the error = null, then we've loaded the file successfully + if( err === null ) { - // status code: https://httpstatuses.com - response.writeHeader( 200, { 'Content-Type': type }) - response.end( content ) + // status code: https://httpstatuses.com + response.writeHeader( 200, { 'Content-Type': type }) + response.end( content ) - }else{ + }else{ - // file not found, error code 404 - response.writeHeader( 404 ) - response.end( '404 Error: File Not Found' ) + // file not found, error code 404 + response.writeHeader( 404 ) + response.end( '404 Error: File Not Found' ) - } - }) + } + }) } server.listen( process.env.PORT || port ) From 57b0ef962891bc8b753067c7da318767a4a95710 Mon Sep 17 00:00:00 2001 From: Jacob van Steyn Date: Fri, 9 Sep 2022 16:55:55 -0400 Subject: [PATCH 3/3] fix --- public/css/style.css | 1 - public/index.html | 98 ++++++++------------------------------------ public/js/scripts.js | 78 ++++++++++++++++++++++++++++++++++- server.improved.js | 16 ++++---- 4 files changed, 104 insertions(+), 89 deletions(-) diff --git a/public/css/style.css b/public/css/style.css index 302f6f4..18e8d08 100644 --- a/public/css/style.css +++ b/public/css/style.css @@ -16,7 +16,6 @@ html { padding-left: 10px; padding-right: 10px; color: #333; - height: 90vh; margin-top: 5vh; margin-bottom: 5vh; overflow-y: auto; diff --git a/public/index.html b/public/index.html index dd67611..6808ddc 100644 --- a/public/index.html +++ b/public/index.html @@ -3,6 +3,8 @@ + + CS4241 Assignment 2 @@ -14,7 +16,7 @@

- +