-
Notifications
You must be signed in to change notification settings - Fork 2
/
handler.js
81 lines (73 loc) · 2.25 KB
/
handler.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
const fs = require('fs');
const path = require('path');
const queryString = require('query-string');
const places = require('../places.json');
// function handler (request, response) {
const handleQuery = (request, response) => {
let filtered = [];
let searchString = queryString.parse(request.url)['/query'].toLowerCase();
let newArray = places['cities'].map(city1 => {
if (city1.city.toLowerCase().includes(searchString)) {
return filtered.push(city1.city.toLowerCase());
}
});
const firstEight = filtered.slice(0, 8);
// console.log(firstEight);
response.writeHead(200, { 'Content-Type': 'application/json' });
response.end(JSON.stringify(firstEight));
};
const handleHomeRoute = (request, response) => {
const filePath = path.join(__dirname, '..', 'public', 'index.html');
fs.readFile(filePath, (error, file) => {
if (error) {
console.log(error);
response.writeHead(500, { 'Content-Type': 'text/html' });
response.end("<h1>Sorry, we've had a problem on our end</h1>");
} else {
response.writeHead(200, { 'Content-Type': 'text/html' });
response.end(file);
}
});
};
const handlePublic = (request, response, url) => {
console.log(request.method);
const extension = url.split('.')[1];
const extensionType = {
html: 'text/html',
css: 'text/css',
js: 'application/javascript',
ico: 'image/x-icon'
};
// Replaced err with error in line 30
const filePath = path.join(__dirname, '..', url);
fs.readFile(filePath, (error, file) => {
if (error) {
console.log(error);
response.writeHead(404, { 'Content-Type': 'text/html' });
response.end('<h1>404 file not found</h1>');
} else {
response.writeHead(200, { 'Content-Type': extensionType[extension] });
response.end(file);
}
});
console.log(url);
};
// if (request.method === "POST") {
// var allTheData = '';
// request.on('data', (chunkOfData) => {
// allTheData += chunkOfData;
// });
// request.on('end', () => {
// const convertedData = querystring.parse(allTheData);
// console.log(convertedData);
// response.writeHead(302, {"Location": "/"});
// response.end();
// })
// }
// };
module.exports = {
// handler
handleHomeRoute,
handlePublic,
handleQuery
};