forked from nerdydrew/Random-Pokemon-Generator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
backend.js
108 lines (100 loc) · 3.97 KB
/
backend.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
const http = require('http');
const fs = require('fs');
const api = require('./api/api')
let path = ".";
const server = http.createServer(function (req, res) {
path = "." + req.url;
console.log(`\${req.method} request received at \${req.url}`);
if (req.url === '/') {
res.setHeader('Content-Type', 'text/html');
res.statusCode = 200; // 200 = OK
displayHTML(res);
res.end();
// JSON Files
} else if (req.url.match(/api\?/g)) {
res.setHeader('Content-Type', 'application/json');
res.statusCode = 200; // 200 = OK
let splits = req.url.split("?");
let return_json = JSON.stringify(api.getRandomPokemonJson(splits[1]), null, 1);
res.write(return_json);
res.end();
} else if (req.url.match(/\.json/g)) {
fs.access(path, fs.constants.F_OK | fs.constants.R_OK, (err) => {
console.log(`${path} ${err ? 'does not exist' : 'exists'}`);
if(err === null) {
res.setHeader('Content-Type', 'application/json');
res.statusCode = 200; // 200 = OK
res.write(fs.readFileSync(path));
res.end();
} else {
res.setHeader('Content-Type', 'text/html');
res.statusCode = 400; // 400 = Bad request
res.write("<h1>Sorry, this page is not available</h1>");
res.end();
}
});
}
//Images
else if (req.url.match(/\.png/g)) {
fs.access(path, fs.constants.F_OK | fs.constants.R_OK, (err) => {
console.log(`${path} ${err ? 'does not exist' : 'exists'}`);
if(err === null) {
res.setHeader('Content-Type', 'image/png');
res.statusCode = 200; // 200 = OK
res.write(fs.readFileSync(path));
res.end();
} else {
res.setHeader('Content-Type', 'text/html');
res.statusCode = 400; // 400 = Bad request
res.write("<h1>Sorry, this page is not available</h1>");
res.end();
}
});
}
// CSS Files
else if (req.url.match(/\.css/g)) {
fs.access(path, fs.constants.F_OK | fs.constants.R_OK, (err) => {
console.log(`${path} ${err ? 'does not exist' : 'exists'}`);
if(err === null) {
res.setHeader('Content-Type', 'text/css');
res.write(fs.readFileSync(path))
res.statusCode = 200;
res.end();
} else {
res.setHeader('Content-Type', 'text/html');
res.statusCode = 400; // 400 = Bad request
res.write("<h1>Sorry, this page is not available</h1>");
res.end();
}
});
// Javascript
} else if (req.url.match(/\.js/g)) {
fs.access(path, fs.constants.F_OK | fs.constants.R_OK, (err) => {
console.log(`${path} ${err ? 'does not exist' : 'exists'}`);
if(err === null) {
res.setHeader('Content-Type', 'text/javascript');
res.write(fs.readFileSync(path))
res.statusCode = 200;
res.end();
} else {
res.setHeader('Content-Type', 'text/html');
res.statusCode = 400; // 400 = Bad request
res.write("<h1>Sorry, this page is not available</h1>");
res.end();
}
});
}
else {
res.setHeader('Content-Type', 'text/html');
res.statusCode = 400; // 400 = Bad request
res.write("<h1>Sorry, this page is not available</h1>");
res.end();
}
});
server.listen(3000, function () {
console.log("Listening on port http://localhost:3000");
});
function displayHTML(res) {
let data = fs.readFileSync("./index.html");
res.write(data);
}