forked from cs4241-19a/a2-shortstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.improved.js
More file actions
154 lines (120 loc) · 4.89 KB
/
server.improved.js
File metadata and controls
154 lines (120 loc) · 4.89 KB
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
151
152
153
154
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 = [
{ 'name': 'Yeezy 350 Boost', 'category': 'Fashion', 'rating': 5, 'usd': 200, 'eur': 182, 'link': "https://stockx.com/adidas-yeezy-boost-350-v2-cream-white?currencyCode=USD&size=8.5&gclid=CjwKCAjwzdLrBRBiEiwAEHrAYr6Goiw3RGnCl12vXPPsgVqOjI-F36X_4AfNaeBDvt6D-mjEkhmVBBoCRBEQAvD_BwE"},
{ 'name': 'Macbook Pro', 'category': 'Tech', 'rating': 3, 'usd': 1299, 'eur': 1178.31, 'link': "https://www.apple.com/shop/buy-mac/macbook-pro/13-inch-space-gray-1.4ghz-quad-core-processor-with-turbo-boost-up-to-3.9ghz-128gb?afid=p238%7Csbepnohbm-dc_mtid_1870765e38482_pcrid_246386726307_pgrid_14874568330_&cid=aos-us-kwgo-pla-mac--slid-----product-MUHN2LL/A" },
{ 'name': 'Wilson Basketball', 'category': 'Sports', 'rating': 2, 'usd': 20, 'eur': 18.14, 'link': "https://www.wilson.com/en-us/basketball/balls/evolution/evolution-game-basketball?gclid=CjwKCAjwzdLrBRBiEiwAEHrAYsO1rcrobWjAgngQhHvN_RTM9DJZj_zVaqj5c4KJ7Vw5_S4yYuE4QxoCYssQAvD_BwE&source=googleshopping&ef_id=CjwKCAjwzdLrBRBiEiwAEHrAYsO1rcrobWjAgngQhHvN_RTM9DJZj_zVaqj5c4KJ7Vw5_S4yYuE4QxoCYssQAvD_BwE:G:s&s_kwcid=AL!8492!3!179840140943!!!g!430754648574!&CMPID=Google-wilson-basketball_g_shopping_usa---c-179840140943-" }
]
const server = http.createServer( function( request,response ) {
if( request.method === 'GET' ) {
handleGet( request, response )
} else if( request.method === 'POST' ){
handlePost( request, response )
} else if( request.method === 'DELETE' ){
handleDelete( request, response )
} else {
handlePut( request, response )
}
})
const handleGet = function( request, response ) {
const filename = dir + request.url.slice( 1 )
if( request.url === '/' ) {
sendFile( response, 'public/index.html' )
} else if ( request.url === '/items' ){
sortData();
sendData( response, appdata );
}
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 item = JSON.parse( dataString );
const newItemObj = {
'name': item.name,
'category': item.category,
'rating': parseInt(item.rating),
'usd': parseFloat(item.usd),
'eur': calcEuroPrice(parseFloat(item.usd)),
'link': item.link,
};
appdata.push(newItemObj);
sortData();
response.writeHead( 200, "OK", {'Content-Type': 'text/plain' })
response.end()
})
}
const handleDelete = function( request, response ) {
let dataString = ''
request.on( 'data', function( data ) {
dataString += data
})
request.on( 'end', function() {
console.log( JSON.parse( dataString ) )
const deleteItem = JSON.parse(dataString);
appdata.splice(deleteItem.index, 1);
sortData();
response.writeHead( 200, "OK", {'Content-Type': 'text/plain' })
response.end()
})
}
const handlePut = function( request, response ) {
let dataString = ''
request.on( 'data', function( data ) {
dataString += data
})
request.on( 'end', function() {
const updateItem = JSON.parse(dataString);
const updatedItemObj = {
'name': updateItem.name,
'category': updateItem.category,
'rating': parseInt(updateItem.rating),
'usd': parseFloat(updateItem.usd),
'eur': calcEuroPrice(parseFloat(updateItem.usd)),
'link': updateItem.link,
};
appdata.splice(updateItem.index, 1, updatedItemObj);
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' )
}
})
}
const sendData = function( response, items ) {
const type = mime.getType( items );
response.writeHeader(200, { 'Content-Type': type });
response.write(JSON.stringify({ data: items }));
response.end();
};
function calcEuroPrice(usd) {
return (usd * 0.91).toFixed(2);
}
function sortData() {
// sort the data to ensure favorite are always first 3 elements
appdata.sort((a, b) => (a.rating < b.rating) ? 1 : (a.rating === b.rating) ? ((a.usd > b.usd) ? 1 : -1) : -1 );
}
server.listen( process.env.PORT || port )