-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsite.js
76 lines (74 loc) · 2.52 KB
/
website.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
const http = require("http")
const fs = require('fs')
const path = require('path')
const { getImageLink } = require('./scrapper.js');
const { getAPIKEY } = require('./getEnv.js');
const host = 'localhost'
const port = 8000
const requestListener = async function (req, res) {
if (req.url === '/') {
fs.readFile(path.join(__dirname, 'index.html'), 'utf8', (err, data) => {
if (err) {
res.writeHead(500);
res.end('Error loading HTML file');
return;
}
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(data);
});
} else if (req.url === '/getImageLink') {
try {
const data = await getImageLink();
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ data }));
} catch (error) {
console.error('Error:', error);
res.writeHead(500);
res.end('Internal Server Error');
}
} else if(req.url === '/maps') {
try {
const data = await getAPIKEY();
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end({data});
} catch (error) {
console.error('Error:', error);
res.writeHead(500);
res.end('Internal Server Error');
}
} else if (req.url == '/ImageGallery.js') {
fs.readFile('./ImageGallery.js', function (err, jsFile) {
if (err) {
res.send(500, { error: err });
}
res.writeHeader(200, { "Content-Type": "text/javascript" });
res.write(jsFile);
res.end();
});
}else if (req.url == '/logic.js') {
fs.readFile('./logic.js', function (err, jsFile) {
if (err) {
res.send(500, { error: err });
}
res.writeHeader(200, { "Content-Type": "text/javascript" });
res.write(jsFile);
res.end();
});
} else if (req.url == '/style.css') {
fs.readFile('./style.css', function (err, css) {
if (err) {
res.send(500, { error: err });
}
res.writeHeader(200, { "Content-Type": "text/css" });
res.write(css);
res.end();
});
} else {
res.writeHead(404);
res.end('Page not found');
}
};
const server = http.createServer(requestListener);
server.listen(port, host, () => {
console.log(`Server is running on http://${host}:${port}`);
});