-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpreview-server.js
More file actions
40 lines (34 loc) · 1.03 KB
/
Copy pathpreview-server.js
File metadata and controls
40 lines (34 loc) · 1.03 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
const http = require('http');
const fs = require('fs');
const path = require('path');
const root = __dirname;
const mime = {
'.html': 'text/html; charset=utf-8',
'.css': 'text/css; charset=utf-8',
'.js': 'text/javascript; charset=utf-8',
'.png': 'image/png',
'.svg': 'image/svg+xml'
};
const server = http.createServer((req, res) => {
const urlPath = decodeURIComponent(req.url.split('?')[0]);
const file = urlPath.startsWith('/node_modules/')
? path.join(root, urlPath.slice(1))
: path.join(root, 'src', 'renderer', urlPath === '/' ? 'index.html' : urlPath.replace(/^\//, ''));
if (!file.startsWith(root)) {
res.writeHead(403);
res.end('Forbidden');
return;
}
fs.readFile(file, (error, data) => {
if (error) {
res.writeHead(404);
res.end('Not found');
return;
}
res.writeHead(200, { 'Content-Type': mime[path.extname(file)] || 'application/octet-stream' });
res.end(data);
});
});
server.listen(4177, '127.0.0.1', () => {
console.log('preview http://127.0.0.1:4177');
});