Create server.js
Browse files
server.js
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const http = require('http');
|
| 3 |
+
const { Server } = require('socket.io');
|
| 4 |
+
|
| 5 |
+
const app = express();
|
| 6 |
+
const server = http.createServer(app);
|
| 7 |
+
const io = new Server(server);
|
| 8 |
+
|
| 9 |
+
const logs = [];
|
| 10 |
+
const MAX_LOGS = 10000;
|
| 11 |
+
|
| 12 |
+
// ===== SOCKET =====
|
| 13 |
+
io.on('connection', (socket) => {
|
| 14 |
+
console.log('⚡ Client connected:', socket.id);
|
| 15 |
+
|
| 16 |
+
socket.on('log', (data) => {
|
| 17 |
+
const log = {
|
| 18 |
+
...data,
|
| 19 |
+
receivedAt: new Date()
|
| 20 |
+
};
|
| 21 |
+
|
| 22 |
+
logs.push(log);
|
| 23 |
+
|
| 24 |
+
if (logs.length > MAX_LOGS) logs.shift();
|
| 25 |
+
|
| 26 |
+
// broadcast to UI
|
| 27 |
+
io.emit('log', log);
|
| 28 |
+
});
|
| 29 |
+
|
| 30 |
+
socket.on('disconnect', () => {
|
| 31 |
+
console.log('❌ Client disconnected');
|
| 32 |
+
});
|
| 33 |
+
});
|
| 34 |
+
|
| 35 |
+
// ===== API (JSON logs) =====
|
| 36 |
+
app.get('/api/logs', (req, res) => {
|
| 37 |
+
res.json(logs);
|
| 38 |
+
});
|
| 39 |
+
|
| 40 |
+
// ===== CONSOLE UI =====
|
| 41 |
+
app.get('/logs', (req, res) => {
|
| 42 |
+
res.send(`
|
| 43 |
+
<!DOCTYPE html>
|
| 44 |
+
<html>
|
| 45 |
+
<head>
|
| 46 |
+
<title>Live Logs</title>
|
| 47 |
+
<style>
|
| 48 |
+
body {
|
| 49 |
+
background: #0f172a;
|
| 50 |
+
color: #e2e8f0;
|
| 51 |
+
font-family: monospace;
|
| 52 |
+
padding: 10px;
|
| 53 |
+
}
|
| 54 |
+
.log { margin: 2px 0; }
|
| 55 |
+
.info { color: #38bdf8; }
|
| 56 |
+
.error { color: #ef4444; }
|
| 57 |
+
.warn { color: #facc15; }
|
| 58 |
+
.debug { color: #a78bfa; }
|
| 59 |
+
.time { color: #64748b; margin-right: 8px; }
|
| 60 |
+
</style>
|
| 61 |
+
</head>
|
| 62 |
+
<body>
|
| 63 |
+
<h3>📡 Live Logs</h3>
|
| 64 |
+
<div id="logs"></div>
|
| 65 |
+
|
| 66 |
+
<script src="/socket.io/socket.io.js"></script>
|
| 67 |
+
<script>
|
| 68 |
+
const socket = io();
|
| 69 |
+
|
| 70 |
+
const logsDiv = document.getElementById('logs');
|
| 71 |
+
|
| 72 |
+
function addLog(log) {
|
| 73 |
+
const el = document.createElement('div');
|
| 74 |
+
el.className = 'log ' + (log.level || 'info');
|
| 75 |
+
|
| 76 |
+
const time = new Date(log.timestamp || log.receivedAt).toLocaleTimeString();
|
| 77 |
+
|
| 78 |
+
el.innerHTML = \`
|
| 79 |
+
<span class="time">[\${time}]</span>
|
| 80 |
+
<span>\${log.message}</span>
|
| 81 |
+
\`;
|
| 82 |
+
|
| 83 |
+
logsDiv.appendChild(el);
|
| 84 |
+
window.scrollTo(0, document.body.scrollHeight);
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
// initial logs
|
| 88 |
+
fetch('/api/logs')
|
| 89 |
+
.then(res => res.json())
|
| 90 |
+
.then(data => data.forEach(addLog));
|
| 91 |
+
|
| 92 |
+
// live logs
|
| 93 |
+
socket.on('log', addLog);
|
| 94 |
+
</script>
|
| 95 |
+
|
| 96 |
+
</body>
|
| 97 |
+
</html>
|
| 98 |
+
`);
|
| 99 |
+
});
|
| 100 |
+
|
| 101 |
+
// ===== START =====
|
| 102 |
+
const PORT = 6000;
|
| 103 |
+
|
| 104 |
+
server.listen(PORT, () => {
|
| 105 |
+
console.log('🚀 Log server running on port', PORT);
|
| 106 |
+
});
|