From 4c2b235de7cd5eed88b1bb4712564921fc0df90f Mon Sep 17 00:00:00 2001 From: Benny Klaiman <47163915+bnklaiman@users.noreply.github.com> Date: Wed, 16 Sep 2020 23:16:53 -0400 Subject: [PATCH 01/12] Update server.improved.js --- server.improved.js | 135 +++++++++++++++++++++++++-------------------- 1 file changed, 75 insertions(+), 60 deletions(-) diff --git a/server.improved.js b/server.improved.js index 26673fc0..38b57823 100644 --- a/server.improved.js +++ b/server.improved.js @@ -1,72 +1,87 @@ -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 +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; const appdata = [ - { 'model': 'toyota', 'year': 1999, 'mpg': 23 }, - { 'model': 'honda', 'year': 2004, 'mpg': 30 }, - { 'model': 'ford', 'year': 1987, 'mpg': 14} -] + { + task: "Finish Laundry", + priority: "low", + isTaskComplete: true, + deadline: new Date(1984, 11, 22) + }, + { + task: "Check office hours", + priority: "high", + isTaskComplete: false, + deadline: new Date(2012, 1, 4) + }, + { + task: "Start coding", + priority: "high", + isTaskComplete: true, + deadline: new Date(2019, 6, 24) + } +]; -const server = http.createServer( function( request,response ) { - if( request.method === 'GET' ) { - handleGet( request, response ) - }else if( request.method === 'POST' ){ - handlePost( request, response ) +const server = http.createServer(function(request, response) { + if (request.method === "GET") { + handleGet(request, response); + } else if (request.method === "POST") { + handlePost(request, response); } -}) +}); -const handleGet = function( request, response ) { - const filename = dir + request.url.slice( 1 ) +const handleGet = function(request, response) { + const filename = dir + request.url.slice(1); - if( request.url === '/' ) { - sendFile( response, 'public/index.html' ) - }else{ - sendFile( response, filename ) + if (request.url === "/") { + sendFile(response, "public/index.html"); + } else { + sendFile(response, filename); } -} +}; -const handlePost = function( request, response ) { - let dataString = '' +const handlePost = function(request, response) { + let dataString = ""; - request.on( 'data', function( data ) { - dataString += data - }) + request.on("data", function(data) { + console.log() + dataString += data; + }); - request.on( 'end', function() { - console.log( JSON.parse( dataString ) ) + request.on("end", function() { + console.log(JSON.parse(dataString)); // ... do something with the data here!!! - - response.writeHead( 200, "OK", {'Content-Type': 'text/plain' }) - response.end() - }) -} - -const sendFile = function( response, filename ) { - const type = mime.getType( filename ) - - fs.readFile( filename, function( err, content ) { - - // 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 ) - - }else{ - - // file not found, error code 404 - response.writeHeader( 404 ) - response.end( '404 Error: File Not Found' ) - - } - }) -} - -server.listen( process.env.PORT || port ) + // appdata.forEach(element => console.log(element)); + + console.log("New data: "); + + + response.writeHead(200, "OK", { "Content-Type": "text/plain" }); + response.end(); + }); +}; + +const sendFile = function(response, filename) { + const type = mime.getType(filename); + + fs.readFile(filename, function(err, content) { + // 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); + } else { + // file not found, error code 404 + response.writeHeader(404); + response.end("404 Error: File Not Found"); + } + }); +}; + +server.listen(process.env.PORT || port); From c69135f118af8cdce176fb8ccda7f456b924c029 Mon Sep 17 00:00:00 2001 From: Benny Klaiman <47163915+bnklaiman@users.noreply.github.com> Date: Wed, 16 Sep 2020 23:17:20 -0400 Subject: [PATCH 02/12] Update README.md --- README.md | 82 ++++++------------------------------------------------- 1 file changed, 8 insertions(+), 74 deletions(-) diff --git a/README.md b/README.md index 642c55c4..392c3fe0 100755 --- a/README.md +++ b/README.md @@ -1,79 +1,13 @@ -Assignment 2 - Short Stack: Basic Two-tier Web Application using HTML/CSS/JS and Node.js -=== +Benny Klaiman +http://a2-bnklaiman.glitch.me -Due: September 16th, by 11:59 AM. +This project shows Assignment 2 for CS 4241: Webware in the form of a to-do list. -This assignment aims to introduce you to the concepts and practice involved in creating a prototype (i.e. not deployment ready) two-tiered web application. The baseline aims of this assignment involve creating an application that demonstrates the use of several specific pieces of HTML, CSS, JavaScript, and Node.js functionality. - -Baseline Requirements ---- - -Note that there is a very large range of application areas and possibilities that meet these baseline requirements. Make your application do something useful! A todo list, storing / retrieving high scores for a very simple game, have a little fun with it. - -Your application is required to implement the following functionalities: - -- a `Server` which not only serves files, but also maintains a tabular dataset with 3 or more fields related to your application -- a `Results` functionality which shows the entire dataset residing in the server's memory -- a `Form/Entry` functionality which allows a user to add, modify, or delete data items residing in the server's memory -- a `Server Logic` which, upon receiving new or modified "incoming" data, includes and uses a function that adds at least one additional derived field to this incoming data before integrating it with the existing dataset -- the `Derived field` for a new row of data must be computed based on fields already existing in the row. For example, a `todo` dataset with `task`, `priority`, and `creation_date` may generate a new field `deadline` by looking at `creation_date` and `priority` - -Your application is required to demonstrate the use of the following concepts: - -HTML: -- One or more [HTML Forms](https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms), with any combination of form tags appropriate for the user input portion of the application -- A results page displaying all data currently available on the server. You will most likely use a `` tag for this, but `