Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
50 lines (44 loc) · 1.37 KB
/
Copy pathserver.js
File metadata and controls
50 lines (44 loc) · 1.37 KB
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
const http = require('http');
const fs = require('fs');
const url = require('url');
const port = 8080;
function sendFile(res, filename, contentType) {
contentType = contentType || 'text/html';
fs.readFile(filename, (error, content) => {
res.writeHead(200, { 'Content-Type': contentType });
res.end(content, 'utf-8');
});
}
const server = http.createServer((req, res) => {
const uri = url.parse(req.url);
if (uri.pathname.includes('assets')) {
let path = uri.pathname;
path = path.substr(path.indexOf('assets'));
console.log(path);
let mime = 'text';
if (path.includes('.css')) {
mime = 'text/css';
} else if (path.includes('.js')) {
mime = 'application/javascript';
} else if (path.includes('.jpeg') || path.includes('.jpg')) {
mime = 'image/jpeg';
} else if (path.includes('.png')) {
mime = 'image/png';
} else if (path.includes('.svg')) {
mime = 'image/svg+xml';
}
sendFile(res, path, mime);
} else if (uri.pathname.includes('csv')) {
let path = uri.pathname;
path = path.substr(path.indexOf('csv'));
sendFile(res, path, 'text');
} else if (uri.pathname === '/import') {
console.log('/import');
sendFile(res, 'import.html');
} else {
console.log('/');
sendFile(res, 'index.html');
}
});
server.listen(process.env.PORT || port);
console.log('listening on 8080');