diff --git a/.gitignore b/.gitignore
index 57195033..a8d703f5 100755
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
*.DS_Store
node_modules/
package-lock.json
+.vscode/
\ No newline at end of file
diff --git a/README.md b/README.md
index 3945c152..bfa04f5c 100755
--- a/README.md
+++ b/README.md
@@ -1,93 +1,23 @@
-Assignment 2 - Short Stack: Basic Two-tier Web Application using HTML/CSS/JS and Node.js
-===
+# Alex's Training Log
-Due: September 16th, by 11:59 PM.
+## Description
-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.
+[https://a2-afsimoneau.glitch.me/](https://a2-afsimoneau.glitch.me/)
-Baseline Requirements
----
+I created this project to be a running log for my daily training. I centered all of the major CSS elements in a grid layout and margin centering. It calculates a relative fitness score using the distance traveled and the time taken to approximate the training effect of a run. The form checks if you submit empty data and raises alerts to avoid blank fields in the application data
-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 `
` could also work and might be simpler to work with.
-- All pages should [validate](https://validator.w3.org)
-
-CSS:
-- CSS styling of the primary visual elements in the application
-- Various CSS Selector functionality must be demonstrated:
- - Element selectors
- - ID selectors
- - Class selectors
-- CSS positioning and styling of the primary visual elements in the application:
- - Use of either a CSS grid or flexbox for layout
- - Rules defining fonts for all text used; no default fonts! Be sure to use a web safe font or a font from a web service like [Google Fonts](http://fonts.google.com/)
-
-- CSS defined in a maintainable, readable form, in external stylesheets
-
-JavaScript:
-- At minimum, a small amount of front-end JavaScript to get / fetch data from the server; a sample is provided in this repository.
-
-Node.js:
-- An HTTP Server that delivers all necessary files and data for the application. A starting point is provided in this repository.
-
-Deliverables
----
-
-Do the following to complete this assignment and acheive a base grade of 85%:
-
-1. Fork the starting project code (make sure to fork the 2020 repo!). This repo contains some starter code that may be used or discarded as needed.
-2. Implement your project with the above requirements.
-3. Test your project to make sure that when someone goes to your main page, it displays correctly.
-4. Deploy your project to Glitch, and fill in the appropriate fields in your package.json file.
-5. Ensure that your project has the proper naming scheme `a2-yourname` so we can find it.
-6. Modify the README to the specifications below.
-7. Create and submit a Pull Request to the original repo. Label the pull request as follows: a2-gitusername-firstname-lastname
-
-Acheivements
----
-
-Below are suggested technical and design achievements. You can use these to help boost your grade up to an A and customize the assignment to your personal interests. These are recommended acheivements, but feel free to create/implement your own... just make sure you thoroughly describe what you did in your README and why it was challenging. ALL ACHIEVEMENTS MUST BE DESCRIBED IN YOUR README IN ORDER TO GET CREDIT FOR THEM.
-
-*Technical*
-- (10 points) Create a single-page app that both provides a form for users to submit data and always shows the current state of the server-side data. To put it another way, when the user submits data, the server should respond sending back the updated data (including the derived field calculated on the server) and the client should then update its data display.
-
-*Design/UX*
-- (5 points per person, with a max of 10 points) Test your user interface with other students in the class. Define a specific task for them to complete (ideally something short that takes <10 minutes), and then use the [think-aloud protocol](https://en.wikipedia.org/wiki/Think_aloud_protocol) to obtain feedback on your design (talk-aloud is also find). Important considerations when designing your study:
-
-1. Make sure you start the study by clearly stating the task that you expect your user to accomplish.
-2. You shouldn't provide any verbal instructions on how to use your interface / accomplish the task you give them. Make sure that your interface is clear enough that users can figure it out without any instruction, or provide text instructions from within the interface itself.
-3. If users get stuck to the point where they give up, you can then provde instruction so that the study can continue, but make sure to discuss this in your README. You won't lose any points for this... all feedback is good feedback!
-
-You'll need to use sometype of collaborative software that will enable you both to see the test subject's screen and listen to their voice as they describe their thoughts. After completing each study, briefly (one to two sentences for each question) address the following in your README:
-
-1. Provide the last name of each student you conduct the evaluation with.
-2. What problems did the user have with your design?
-3. What comments did they make that surprised you?
-4. What would you change about the interface based on their feedback?
-
-*You do not need to actually make changes based on their feedback*. This acheivement is designed to help gain experience testing user interfaces. If you run two user studies, you should answer two sets of questions.
-
-Sample Readme (delete the above when you're ready to submit, and modify the below so with your links and descriptions)
----
+## Technical Achievements
-## Your Web Application Title
-Include a very brief summary of your project here. Be sure to include the CSS positioning technique you used, and any required instructions to use your application.
+- **Single-page App**: Using POST requests, data is updated from the server on initial page load and on submission.
-## Technical Achievements
-- **Tech Achievement 1**: Using a combination of...
+## Design/Evaluation Achievements
-### Design/Evaluation Achievements
-- **Design Achievement 1**:
+- **Student Client Tests**:
+ - *Alexa Freglette*
+ - ***(issue)*** As route name gets longer, squishes other fields
+ - ***(feedback)*** Expand outward instead of squishing
+ - ***(positive)*** Easy to use and see what's going on
+ - ***(positive)*** Warns if you do it wrong
+ - *Jenna Galli*
+ - ***(positive)*** Like the colors and layout
+ - ***(feedback)*** More clarity on what fitness score means
diff --git a/package.json b/package.json
index 988f135f..e6797336 100755
--- a/package.json
+++ b/package.json
@@ -1,5 +1,5 @@
{
- "name": "",
+ "name": "a2-shortstack",
"version": "",
"description": "",
"author": "",
@@ -7,6 +7,6 @@
"start": "node server.improved.js"
},
"dependencies": {
- "mime": "^2.4.4"
+ "mime": "^2.4.6"
}
}
diff --git a/public/css/style.css b/public/css/style.css
index d5f842ab..1cd86a27 100755
--- a/public/css/style.css
+++ b/public/css/style.css
@@ -1 +1,74 @@
-/*Style your own assignment! This is fun! */
\ No newline at end of file
+/*Style your own assignment! This is fun! */
+
+h1 {
+ color: #ccffed;
+ width: 25%;
+ margin-left: 35%;
+}
+
+hr {
+ color: #ccffed;
+}
+body {
+ grid-template-rows: auto;
+ align-content: center;
+ font-size: x-large;
+ text-align: center;
+ font-family: "Kumbh Sans", sans-serif;
+ background-color: #455953;
+}
+
+input[type="text"] {
+ border: none;
+ background-color: #4ea68a;
+ margin: 10px 0;
+ padding: 10px 10px;
+ width: 15%;
+ font-size: x-large;
+ color: #ccffed;
+}
+
+button {
+ background-color: #4ea68a;
+ border: none;
+ color: #ccffed;
+ padding: 15px 32px;
+ text-align: center;
+ text-decoration: none;
+ display: inline-flex;
+ font-size: 16px;
+ border-radius: 50px;
+}
+form {
+ padding: 15px;
+ margin: 10px;
+ color: #ccffed;
+ text-align: center;
+}
+
+table {
+ color: #ccffed;
+ margin-left: 20%;
+ margin-right: 20%;
+
+ align-items: center;
+ justify-content: center;
+ border-collapse: collapse;
+ width: 60%;
+ background-color: #4ea68a;
+}
+
+th,
+td,
+tr {
+ padding-top: 12px;
+ padding-bottom: 12px;
+ text-align: center;
+ border: 2px solid #ccffed;
+}
+
+thead,
+table td:nth-child(1) {
+ /*highlight header row and first column*/
+ background-color: #2a594a;
+}
diff --git a/public/index.html b/public/index.html
index c56d620e..53f24011 100755
--- a/public/index.html
+++ b/public/index.html
@@ -1,41 +1,40 @@
-
-
+
+
+
+
+
CS4241 Assignment 2
-
+
-
+
+
+
+
+
Route
+
Time
+
Distance
+
Fitness Score
+
+
+
+
-
diff --git a/public/js/scripts.js b/public/js/scripts.js
index de052eae..b7b1601f 100755
--- a/public/js/scripts.js
+++ b/public/js/scripts.js
@@ -1,3 +1,68 @@
-// Add some Javascript code here, to run on the front end.
+const loadTable = function (jsonData) {
+ var table = document.getElementById("resultTableBody");
+ table.innerHTML = "";
+ for (var i = 0; i < jsonData.length; i++) {
+ var tr = table.insertRow(-1);
+ for (var j in jsonData[i]) {
+ var tb = tr.insertCell(-1);
+ tb.innerHTML = jsonData[i][j];
+ }
+ }
+};
-console.log("Welcome to assignment 2!")
\ No newline at end of file
+const submit = function (e) {
+ // prevent default form action from being carried out
+ e.preventDefault();
+
+ //sanitize inputs here
+
+ var inputRoute = document.querySelector("#inputRoute");
+ var inputTime = document.querySelector("#inputTime");
+ var inputDistance = document.querySelector("#inputDistance");
+ if (inputRoute.value === "") {
+ alert("Please input a route");
+ return false;
+ }
+ if (inputTime.value === "") {
+ alert("Please input a time in minutes");
+ return false;
+ }
+ if (inputDistance.value === "") {
+ alert("Please input a distance in miles");
+ return false;
+ }
+
+ const json = {
+ route: inputRoute.value,
+ time: inputTime.value,
+ distance: inputDistance.value,
+ },
+ body = JSON.stringify(json);
+
+ fetch("/submit", {
+ method: "POST",
+ headers: {
+ Accept: "application/json, text/plain, */*",
+ "Content-Type": "application/json",
+ },
+ body,
+ })
+ .then((response) => response.json())
+ .then((jsonData) => loadTable(jsonData));
+
+ return false;
+};
+
+window.onload = function () {
+ fetch("/loadData", {
+ method: "POST",
+ headers: {
+ Accept: "application/json, text/plain, */*",
+ "Content-Type": "application/json",
+ },
+ })
+ .then((response) => response.json())
+ .then((jsonData) => loadTable(jsonData));
+ const button = document.querySelector("button");
+ button.onclick = submit;
+};
diff --git a/server.improved.js b/server.improved.js
index 26673fc0..1e55c1bf 100644
--- a/server.improved.js
+++ b/server.improved.js
@@ -1,72 +1,84 @@
-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}
-]
-
-const server = http.createServer( function( request,response ) {
- if( request.method === 'GET' ) {
- handleGet( request, response )
- }else if( request.method === 'POST' ){
- handlePost( request, response )
+ { route: "Normal Run", time: 30, distance: 4, fitness: 120 },
+ { route: "Workout", time: 15, distance: 2.5, fitness: 37.5 },
+ { route: "Long Run", time: 60, distance: 9, fitness: 540 },
+];
+
+const sendAppData = function (response) {
+ response.writeHead(200, "OK", { "Content-Type": "text/plain" });
+ response.end(JSON.stringify(appdata));
+};
+
+const calculateFitness = function (record) {
+ return record.time * record.distance;
+};
+
+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 = ''
-
- request.on( 'data', function( data ) {
- dataString += data
- })
-
- 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 )
+const handlePost = function (request, response) {
+ if (request.url === "/loadData") {
+ sendAppData(response);
+ return true;
+ }
+ let dataAsString = "";
+ request.on("data", function (dataBuffer) {
+ dataAsString += dataBuffer;
+ });
+ //parse data buffer as string
+ request.on("end", function () {
+ if (request.url === "/submit") {
+ var newRecord = JSON.parse(dataAsString); //get new record json
+ appdata.push({
+ route: newRecord.route,
+ time: newRecord.time,
+ distance: newRecord.distance,
+ fitness: calculateFitness(newRecord),
+ }); //put record data into "database"
+ }
+ sendAppData(response);
+ });
+ return true;
+};
+
+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);