-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
76 lines (65 loc) · 2.51 KB
/
server.js
File metadata and controls
76 lines (65 loc) · 2.51 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const http = require('http');
const fs = require('fs');
const path = require('path');
const PORT = 8000;
const server = http.createServer((req, res) => {
console.log(`${new Date().toISOString()} - ${req.method} ${req.url}`);
// More comprehensive CORS headers
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', 'true');
if (req.method === 'OPTIONS') {
res.writeHead(200);
res.end();
return;
}
// Strip query string, then resolve to an absolute path within the project root
const urlPath = req.url.split('?')[0];
const projectRoot = path.resolve(__dirname);
let filePath = path.resolve(projectRoot, '.' + urlPath);
// Prevent path traversal: ensure resolved path stays within the project root
if (!filePath.startsWith(projectRoot)) {
res.writeHead(403, { 'Content-Type': 'text/plain' });
res.end('403 Forbidden');
return;
}
if (filePath === projectRoot || filePath === projectRoot + path.sep) {
filePath = path.join(projectRoot, 'index.html');
}
const extname = String(path.extname(filePath)).toLowerCase();
const mimeTypes = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon'
};
const contentType = mimeTypes[extname] || 'application/octet-stream';
fs.readFile(filePath, (error, content) => {
if (error) {
console.error(`Error reading file ${filePath}:`, error);
if(error.code == 'ENOENT') {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('404 - File Not Found');
} else {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('500 - Server Error');
}
} else {
res.writeHead(200, { 'Content-Type': contentType });
res.end(content, 'utf-8');
console.log(`Successfully served: ${filePath}`);
}
});
});
server.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}/`);
console.log(`Manifest URL: http://localhost:${PORT}/manifest.json`);
console.log(`Extension URL: http://localhost:${PORT}/index.html`);
console.log('\nPress Ctrl+C to stop the server\n');
});