-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
71 lines (64 loc) · 1.99 KB
/
app.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
const http = require('http');
const fs = require('fs');
const url = require('url');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
const pathname = url.parse(req.url).pathname;
const extension = pathname.split('.').pop();
const dirs = pathname.split('/');
let file = `.${pathname}`,
isImage = false,
contentType,
fileToLoad;
if (pathname === '/') {
file = 'index.html';
contentType = 'text/html';
isImage = 2;
} else if (dirs[1] !== 'hidden' && pathname !== '/app.js') {
switch (extension) {
case 'ico':
contentType = 'image/jpg';
isImage = true;
break;
case 'jpg':
contentType = 'image/jpg';
isImage = true;
break;
case 'png':
contentType = 'image/png';
isImage = true;
break;
case 'js':
contentType = 'text/javascript';
isImage = false;
break;
case 'json':
contentType = 'application/json';
isImage = false;
break;
case 'css':
contentType = 'text/css';
isImage = false;
break;
case 'html':
contentType = 'text/html';
isImage = false;
break;
}
}
if (isImage) {
fileToLoad = fs.readFileSync(file);
res.writeHead(200, { 'Content-Type': contentType, 'Retry-After': 3600 });
res.end(fileToLoad, 'binary');
} else {
fileToLoad = fs.readFileSync(file, 'utf8');
res.writeHead(200, { 'Content-Type': contentType, 'Retry-After': 3600 });
res.write(fileToLoad);
res.end();
}
});
server.listen(port, hostname, () => {
console.info(`Server running at http://${hostname}:${port}/`);
});