-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·41 lines (37 loc) · 1.08 KB
/
index.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
const fs = require('fs');
const http = require('http');
const contentTypes = {
'css': 'text/css',
'html': 'text/html',
'ico': 'image/x-icon',
'png': 'image/png',
'js': 'application/javascript'
};
const httpServer = http.createServer(function(req, res) {
server(req, res);
}).listen(process.env.PORT || 5000);
const server = function(req, res) {
const contentType = req.url.split('.').pop();
if (contentTypes[contentType]){
const filename = req.url.split('/').pop();
fs.readFile('dist/coronavirus-tracker/' + (req.url.split('/')[1] === 'assets' ? 'assets/' : '') + filename, function(err, content) {
if (err) {
throw new Error(err);
} else {
res.writeHeader(200, {'Context-Type': contentTypes[contentType]});
res.write(content);
res.end();
}
});
} else {
fs.readFile('dist/coronavirus-tracker/index.html', function(err, html) {
if (err) {
throw new Error(err);
} else {
res.writeHeader(200, {'Context-Type': contentTypes.html});
res.write(html);
res.end();
}
});
}
}