const http = require('http'); const fs = require('fs'); const path = require('path'); const PORT = 3000; // Server-side state let currentNumber = 1; let isRunning = true; // Generate random number between 1-100 function getRandomNumber() { return Math.floor(Math.random() * 100) + 1; } // Update counter independently every ~1 second function startCounterLoop() { function update() { if (isRunning) { currentNumber = getRandomNumber(); console.log('Counter updated to:', currentNumber); // Random jitter between 800-1200ms const jitter = Math.random() * 400 - 200; setTimeout(update, 1000 + jitter); } } setTimeout(update, 500); } // Start the counter loop startCounterLoop(); const server = http.createServer((req, res) => { // Set CORS headers res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS'); res.setHeader('Access-Control-Allow-Headers', 'Content-Type'); if (req.method === 'OPTIONS') { res.writeHead(200); res.end(); return; } // API endpoint to get current counter value if (req.url === '/api/counter') { res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ success: true, value: currentNumber, timestamp: Date.now() })); return; } // Serve the counter display page if (req.url === '/' || req.url === '/counter') { res.writeHead(200, { 'Content-Type': 'text/html' }); res.end(` Random Counter

Random Number Counter

--
Server-side generation • Updates every second
`); return; } // Serve the scraper/viewer page if (req.url === '/viewer' || req.url === '/scraper') { res.writeHead(200, { 'Content-Type': 'text/html' }); res.end(` Counter Viewer

Counter Viewer

--
Connecting...
Updates: 0
Average: --
Min / Max: -- / --
`); return; } // 404 for other routes res.writeHead(404, { 'Content-Type': 'text/plain' }); res.end('Not Found\n\nAvailable routes:\n /counter - Counter display\n /viewer - Counter viewer with stats\n /api/counter - API endpoint'); }); server.listen(PORT, () => { console.log(`Server running on http://localhost:${PORT}`); console.log(`Counter display: http://localhost:${PORT}/counter`); console.log(`Counter viewer: http://localhost:${PORT}/viewer`); console.log(`API endpoint: http://localhost:${PORT}/api/counter`); });