-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathserver.js
More file actions
61 lines (47 loc) · 1.5 KB
/
Copy pathserver.js
File metadata and controls
61 lines (47 loc) · 1.5 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
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env node
//
// Voxel Builder Server
const http = require('http');
const url = require('url');
const fs = require('fs');
const PORT = 8011;
let filePath = undefined;
const mimeTypes = {
'html': 'text/html',
'js': 'text/javascript',
'json': 'application/json',
'png': 'image/png',
'jpg': 'image/jpg',
'svg': 'image/svg+xml',
'ttf': 'font/ttf',
'woff2': 'font/woff2'
};
http.createServer((req, res) => {
const parsedUrl = url.parse(req.url, true);
filePath = `${parsedUrl.pathname}`;
if (parsedUrl.pathname == '/') {
filePath = 'src/index.html';
} else if (parsedUrl.pathname.startsWith('/user')) {
filePath = `.${parsedUrl.pathname}`;
} else {
filePath = `src${parsedUrl.pathname}`;
}
fs.readFile(filePath, (err, data) => {
if (err) {
console.log(`GET 404 -- ${parsedUrl.pathname} -> ${filePath}`)
res.writeHead(404, { 'Content-Type': 'text/html' });
return res.end("404 Not Found");
}
console.log(`GET 200 OK ${parsedUrl.pathname} -> ${filePath}`)
res.writeHead(200, { 'Content-Type': getContentType(filePath) });
res.write(data);
return res.end();
});
}).listen(PORT, () => {
console.log('Voxel Builder')
console.log(`Server running at http://localhost:${PORT}`);
});
function getContentType(filePath) {
const extname = String(filePath).split('.').pop().toLowerCase();
return mimeTypes[extname] || 'application/octet-stream';
}