forked from cs4241-20a/a2-shortstack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.improved.js
134 lines (109 loc) · 4.28 KB
/
server.improved.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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
var myArray = [
{ 'foodname': 'broccoli', 'datebought': "September 9, 2020", 'enjoy' : 5, 'size': 'small', 'placement': 'top shelf'},
{ 'foodname': 'spinach', 'datebought': "September 4, 2020", 'enjoy' : 2, 'size': 'large', 'placement': 'middle shelf'},
{ 'foodname': 'cookies', 'datebought': "September 1, 2020", 'enjoy' : 5, 'size': 'medium', 'placement': 'bottom shelf'},
{ 'foodname': 'honey', 'datebought': "September 1, 2020", 'enjoy' : 5, 'size': 'small', 'placement': 'middle shelf'},
]
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 )
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 ) )
const handleFormSubmit = (event) => {
// Stop the form from submitting since we’re handling that with AJAX.
event.preventDefault();
// TODO: Call our function to get the form data.
var x = 4
var foodname = document.getElementById("foodname").value
var datebought =document.getElementById("purchased").value
var enjoy =document.getElementById("enjoyability").value
var size = document.getElementById("size").value
function placement(){
if(size === "small" || enjoy >3){
return "top shelf"
}
if(size === "medium" && enjoy === 2){
return "middle shelf"
}
else{
return "bottom shelf"
}
}
const data = {
foodname,
datebought,
enjoy,
size,
placement: placement(),
dateuseby: "bruh"
}
myArray.push(data);
console.warn('added', {myArray});
let pre = document.querySelector('#msg pre');
var table = document.getElementById('myTable');
if((document.getElementById("size").value === "small") && ((document.getElementById("enjoyability").value)>3))
{
myArray[i].placement = "top shelf";
}
for (var i = myArray.length-1; i < myArray.length; i++){
//var checkoutDate = new Date(); // Thu Jul 21 2016 10:05:13 GMT-0400 (EDT)
//checkoutDate.setDate(myArray[i].datebought.getDate() + 5 );
var row = `<tr>
<td>${myArray[i].foodname}</td>
<td>${myArray[i].datebought}</td>
<td>${myArray[i].enjoy}</td>
<td>${myArray[i].size}</td>
<td>${myArray[i].placement}</td>
<td>${myArray[i].datetouse}</td>
</tr>`
table.innerHTML += row
}
}
document.addEventListener('DOMContentLoaded', ()=>{
document.getElementById('btn').addEventListener('click', handleFormSubmit);
document.getElementById('btn').addEventListener('click', buildTable(myArray));
});
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 )