forked from microsoft/CopilotHackathon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodeserver.js
More file actions
236 lines (203 loc) · 8.72 KB
/
Copy pathnodeserver.js
File metadata and controls
236 lines (203 loc) · 8.72 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// write a nodejs server that will expose a method call "get" that will return the value of the key passed in the query string
// example: http://localhost:3000/get?key=hello
// if the key is not passed, return "key not passed"
// if the key is passed, return "hello" + key
// if the url has other methods, return "method not supported"
// when server is listening, log "server is listening on port 3000"
const http = require('http');
const url = require('url');
const fs = require('fs');
const {validateSpanishDNI, validatePhoneNumber, daysBetweenDates,memoryUsage,zipFile,getEuropeanCountries } = require('./functions');
const server = http.createServer((req, res) => {
const parsedUrl = url.parse(req.url, true);
const path = parsedUrl.pathname;
const trimmedPath = path.replace(/^\/+|\/+$/g, '');
const method = req.method.toLowerCase();
const queryStringObject = parsedUrl.query;
if (req.url.startsWith('/Validatephonenumber')) {
//get phoneNumber var from querystring
var queryData = url.parse(req.url, true).query;
var phoneNumber = queryData.phoneNumber;
//if phoneNumber is valid return "valid"
if (validatePhoneNumber(phoneNumber)) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('valid');
} else {
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('invalid');
}
}
else
//Calculate days between two dates
//receive by query string 2 parameters date1 and date 2, and calculate the days between those two dates.
if (req.url.startsWith('/daysBetweenDates')) {
//get date1 and date2 var from querystring
var queryData = url.parse(req.url, true).query;
var date1 = queryData.date1;
var date2 = queryData.date2;
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(daysBetweenDates(date1, date2).toString());
}
else
//Receive by querystring a parameter called dni
if (req.url.startsWith('/ValidateSpanishDNI')) {
//get dni var from querystring
var queryData = url.parse(req.url, true).query;
var dni = queryData.dni;
res.end(validateSpanishDNI(dni));
}
else
//Receive by querystring a parameter called color
if(req.url.startsWith('/returnColorCode')){
//get color var from querystring
var queryData = url.parse(req.url, true).query;
var color = queryData.color;
//read colors.json file and return the rgba field
var colors = fs.readFileSync('colors.json', 'utf-8');
colors = JSON.parse(colors);
//find the color in the json file
for (var i = 0; i < colors.length; i++) {
if (colors[i].name == color) {
color = colors[i].rgba;
}
}
//return color code
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(color);
}
else
//if send TellMeAJoke
if(req.url.startsWith('/TellMeAJoke')){
//get joke var from querystring
var queryData = url.parse(req.url, true).query;
var joke = "123"
//Make a call to the joke api and return a random joke using axios
const axios = require('axios');
axios.get('https://official-joke-api.appspot.com/random_joke', { headers: { Accept: 'application/json' } })
.then(response => {
joke = response.data.joke;
})
.catch(error => {
console.log(error);
});
//return joke
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(joke);
}
else
//create a metod MoviesByDirector that get the name of director and return the movies of that director
if(req.url.startsWith('/MoviesByDirector')){
//get director var from querystring
var queryData = url.parse(req.url, true).query;
var director = queryData.director;
//Make a call to the movie api using https://www.omdbapi.com/apikey.aspx and return a list of movies of that director using axios
const axios = require('axios');
axios.get('http://www.omdbapi.com/?apikey=939fa9a6&s=' + director, { headers: { Accept: 'application/json' } })
.then(response => {
director = response.data.Search;
})
.catch(error => {
console.log(error);
});
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(director);
}
else
//created a method ParseUrl that receive by querystring a url and return the host
if(req.url.startsWith('/ParseUrl'))
{
var queryData = url.parse(req.url, true).query;
//get url var from querystring
var get_url = queryData.url;
//Parse the url and return the protocol, host, port, path, querystring and hash
const url_parse = require('url');
const parsedUrl = url_parse.parse(get_url, true);
const host = parsedUrl.host;
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(host);
}
else
//created a method ListFiles that receive by querystring the current directory and return the list of files in a directory
if(req.url.startsWith('/ListFiles'))
{
var queryData = url.parse(req.url, true).query;
//get directory var from querystring
var directory = queryData.directory;
const fs = require('fs');
//iterate the directory and return the list of files
const files = fs.readdirSync(directory);
const files_array = [];
files.forEach(file => {
files_array.push(file);
});
const files_array_json = JSON.stringify(files_array);
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(files_array_json);
}
else
//created a method GetFullTextFile that receive by querystring the filename
if(req.url.startsWith('/GetFullTextFile'))
{
var queryData = url.parse(req.url, true).query;
//get filename var from querystring
var filename = queryData.filename;
//open the filename to read the file
const fs = require('fs');
const file = fs.readFileSync(filename, 'utf8');
const lines = file.split(/\r?\n/);
const lines_array = [];
//iterate the file and return the lines that contains the word "Fusce"
lines.forEach(line => {
if (line.includes("Fusce")) {
lines_array.push(line);
}
}
);
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(lines_array.length.toString());
}
else
//created a method CalculateMemoryConsumption returns the memory consumption of the process in GB, rounded to 2 decimals
if(req.url.startsWith('/CalculateMemoryConsumption'))
{
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(memoryUsage());
}
else
//created a method MakeZipFile that receive by querystring the filename and return the numbers of bytes of the zip file
if(req.url.startsWith('/MakeZipFile'))
{
var queryData = url.parse(req.url, true).query;
//get filename var from querystring
var filename = queryData.filename;
const gzFilename = 'sample.txt.gz';
fileSizeInBytes_string = zipFile(filename, gzFilename);
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(fileSizeInBytes_string);
}
else
//create a method call RandomEuropeanCountry that will return a random european country
if(req.url.startsWith('/RandomEuropeanCountry'))
{
country = getEuropeanCountries()
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(country);
}
else
if (trimmedPath === 'get' && method === 'get') {
const key = queryStringObject.key;
if (key) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(`Hello, ${key}`);
} else {
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('key not passed');
}
} else {
res.writeHead(405, { 'Content-Type': 'text/plain' });
res.end('method not supported');
}
});
server.listen(3000, () => {
console.log('server is listening on port 3000');
});