-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelemetry-server.js
More file actions
71 lines (58 loc) · 2.11 KB
/
Copy pathtelemetry-server.js
File metadata and controls
71 lines (58 loc) · 2.11 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
/**
* 🗄️ Backend de Telemetría (Node.js Puro)
* Escucha en el puerto 4000 y escribe los logs enviados por la SPA
* en un archivo local `sistema_logs.json`. Funciona como una base de datos persistente simple.
*/
import http from 'node:http';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const PORT = 4000;
const LOG_FILE = path.join(__dirname, 'sistema_logs.json');
// Crear el archivo de logs si no existe
if (!fs.existsSync(LOG_FILE)) {
fs.writeFileSync(LOG_FILE, '[]', 'utf8');
}
const server = http.createServer((req, res) => {
// CORS configuration
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
// Handle preflight requests
if (req.method === 'OPTIONS') {
res.writeHead(204);
res.end();
return;
}
// Handle Log POST request
if (req.method === 'POST' && req.url === '/api/logs') {
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
try {
const logEntry = JSON.parse(body);
// Leer el archivo, parsear, hacer append y volver a guardar
const logsActuales = JSON.parse(fs.readFileSync(LOG_FILE, 'utf8'));
logsActuales.push(logEntry);
fs.writeFileSync(LOG_FILE, JSON.stringify(logsActuales, null, 2), 'utf8');
console.log(`[TELEMETRY] Log recibido y guardado: [${logEntry.nivel}]`);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ success: true }));
} catch (err) {
res.writeHead(400);
res.end('Error parsing JSON log');
}
});
} else {
res.writeHead(404);
res.end('Not Found');
}
});
server.listen(PORT, () => {
console.log(`📡 Servidor de Telemetría corriendo en http://localhost:${PORT}`);
console.log(`💾 Los logs se guardarán persistentemente en: ${LOG_FILE}`);
});