-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
56 lines (49 loc) · 1.57 KB
/
server.js
File metadata and controls
56 lines (49 loc) · 1.57 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
const http = require('http');
const fs = require('fs');
const PORT = 8080;
const server = http.createServer((req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, x-trace-id');
if (req.method === 'OPTIONS') {
res.writeHead(204).end();
return;
}
if (req.method === 'GET' && req.url === '/tracker.js') {
fs.readFile('tracker.js', (err, data) => {
if (err) {
res.writeHead(500).end();
} else {
res.writeHead(200, { 'Content-Type': 'application/javascript' }).end(data);
}
});
} else if (req.method === 'POST' && req.url === '/report') {
let body = '';
req.on('data', chunk => (body += chunk));
req.on('end', () => {
const traceId = req.headers['x-trace-id'] || 'unknown-trace-id';
try {
const payload = JSON.parse(body);
// 1. recentLogs einzeln loggen
if (Array.isArray(payload?.recentLogs)) {
payload.recentLogs.forEach(log => {
const line = JSON.stringify({ traceId, ...log }) + '\n';
process.stdout.write(line);
});
}
// 2. error block loggen
if (payload?.error) {
const errorLine = JSON.stringify({ traceId, error: payload.error }) + '\n';
process.stdout.write(errorLine);
}
res.writeHead(200).end();
} catch {
res.writeHead(400).end();
}
});
} else {
res.writeHead(404).end();
}
});
server.listen(PORT, () => {
process.stdout.write(`Listening on http://localhost:${PORT}\n`);
});