forked from cs4241-20a/a2-shortstack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.improved.js
150 lines (124 loc) · 4.56 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const { stringify } = require('querystring')
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 = [
{ firstname: "Joe", lastname: "Bonchon", cameramake: "Canon", cameramodel: "A-1", cameraformat: "35mm", price: 150, condition: 76, bargain: false, delete: false, id: 1 },
{ firstname: "Pongo", lastname: "Pwaga", cameramake: "Nikon", cameramodel: "FTn", cameraformat: "35mm", price: 95, condition: 90, bargain: true, delete: false, id: 2 },
{ firstname: "Toe", lastname: "Progle", cameramake: "Yashica", cameramodel: "Mat124G", cameraformat: "6x6", price: 50, condition: 100, bargain: true, delete: false, id: 3 }
]
// server creation
const server = http.createServer( function( request,response ) {
if( request.method === 'GET' ) { // true for index, JS, CSS, etc.
handleGet( request, response )
}else if( request.method === 'POST' ){ // submitting user data
handlePost( request, response )
}
})
const handleGet = function( request, response ) {
const filename = dir + request.url.slice( 1 )
if( request.url === '/' ) {
sendFile( response, 'public/index.html' )
console.log( "sending webpage" )
} else if( filename === "public/appdata" ){
sendData( response, appdata )
console.log( "sending appdata:" + JSON.stringify( appdata ) )
}else{
sendFile( response, filename )
console.log( "sending the following file: " + filename )
}
}
const handlePost = function( request, response ) {
let dataString = ''
request.on( 'data', function( data ) {
dataString += data
})
request.on( 'end', function() {
incomingData = JSON.parse( dataString )
// let's determine what type of request we're dealing with
// delete by ID
if( incomingData.delete === true ){
incomingData.id = parseInt( incomingData.id )
for( var i = 0; i< appdata.length; i++ ) {
if( incomingData.id === appdata[i].id ) {
appdata.splice( i, 1 )
console.log( "Removed Listing with ID: " + incomingData.id )
break
}
}
}
else {
// convert prices and conditions to ints
incomingData.price = parseInt( incomingData.price )
incomingData.condition = parseInt( incomingData.condition )
// let's check if it's a good bargain
incomingData.bargain = isBargain( incomingData )
// the data has an ID and was not deleted so it must be an update
if( incomingData.id != null ) {
incomingData.id = parseInt( incomingData.id )
for( var i = 0; i< appdata.length; i++ ) {
if( incomingData.id === appdata[i].id ) {
appdata[i] = incomingData
console.log( "Updated Listing" )
break
}
}
}
else
{
// let's assign an ID to this listing
// first let's check if there are any gaps in the IDs
if( appdata[appdata.length - 1].id === appdata.length ){
// the last ID and length matches!
incomingData.id = appdata.length + 1
appdata.push( incomingData )
}else{
for( var i = 0; i < appdata.length; i++ ){
if( appdata[i].id != i + 1 ){
incomingData.id = i + 1
appdata.splice( i, 0, incomingData )
break
}
}
}
}
}
// ... do something with the data here!!!
response.writeHead( 200, "OK", {'Content-Type': 'text/plain' })
response.end()
})
}
const isBargain = function( listing ) {
var bargain = true
if( ( listing.price > listing.condition ) && listing.condition < 50 ) {
bargain = false
}
return bargain
}
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' )
}
})
}
const sendData = function( response, dataname ) {
transmitdata = JSON.stringify( dataname )
// status code: https://httpstatuses.com
response.writeHeader( 200, { 'Content-Type': "text/plain" })
response.write( transmitdata )
response.end()
}
server.listen( process.env.PORT || port )