#!/usr/bin/env node /** * FS25 Web Dashboard - local bridge server * * Serves the static dashboard (../web) and re-reads the mod's live.json file * on every request to /api/live, so the browser can poll it. No dependencies * on purpose - just `node server.js`. */ 'use strict'; const http = require('http'); const fs = require('fs'); const path = require('path'); const os = require('os'); const PORT = process.env.PORT ? Number(process.env.PORT) : 3925; const WEB_DIR = path.join(__dirname, '..', 'web'); // --- Locate the mod's live.json ------------------------------------------- function candidateLiveDataPaths() { const home = os.homedir(); const candidates = []; if (process.env.FS25_LIVE_JSON) { candidates.push(process.env.FS25_LIVE_JSON); } // OneDrive can redirect "Documents" - check both the plain and OneDrive path. candidates.push(path.join(home, 'Documents', 'My Games', 'FarmingSimulator2025', 'modSettings', 'FS25_FarmHUD', 'live.json')); candidates.push(path.join(home, 'OneDrive', 'Documents', 'My Games', 'FarmingSimulator2025', 'modSettings', 'FS25_FarmHUD', 'live.json')); candidates.push(path.join(home, 'OneDrive', 'Dokumente', 'My Games', 'FarmingSimulator2025', 'modSettings', 'FS25_FarmHUD', 'live.json')); return candidates; } function resolveLiveDataPath() { for (const candidate of candidateLiveDataPaths()) { if (candidate && fs.existsSync(candidate)) { return candidate; } } // Fall back to the first guess even if it doesn't exist yet - the file // only appears once you load a savegame with the mod active. return candidateLiveDataPaths()[0]; } let liveDataPath = resolveLiveDataPath(); console.log('[web-dashboard] reading live data from: ' + liveDataPath); // --- Static file serving ---------------------------------------------------- const MIME_TYPES = { '.html': 'text/html; charset=utf-8', '.js': 'application/javascript; charset=utf-8', '.css': 'text/css; charset=utf-8', '.json': 'application/json; charset=utf-8', '.svg': 'image/svg+xml', '.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.ico': 'image/x-icon', }; function serveStatic(req, res) { let requestPath = decodeURIComponent(req.url.split('?')[0]); if (requestPath === '/') { requestPath = '/index.html'; } const fullPath = path.normalize(path.join(WEB_DIR, requestPath)); if (!fullPath.startsWith(WEB_DIR)) { res.writeHead(403); res.end('Forbidden'); return; } fs.readFile(fullPath, (err, data) => { if (err) { res.writeHead(404, { 'Content-Type': 'text/plain' }); res.end('Not found: ' + requestPath); return; } const ext = path.extname(fullPath).toLowerCase(); res.writeHead(200, { 'Content-Type': MIME_TYPES[ext] || 'application/octet-stream' }); res.end(data); }); } function serveLiveData(req, res) { res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Cache-Control', 'no-store'); // re-resolve in case the game (re)creates the file after this server started if (!fs.existsSync(liveDataPath)) { const resolved = resolveLiveDataPath(); if (resolved) { liveDataPath = resolved; } } fs.readFile(liveDataPath, 'utf8', (err, data) => { if (err) { res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ connected: false, error: 'live.json not found yet - start Farming Simulator 25 with the WebDashboard mod active and load a savegame.', expectedPath: liveDataPath, })); return; } res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(data); }); } const server = http.createServer((req, res) => { if (req.url.startsWith('/api/live')) { serveLiveData(req, res); return; } serveStatic(req, res); }); server.listen(PORT, () => { const nets = os.networkInterfaces(); const lanAddresses = []; for (const name of Object.keys(nets)) { for (const net of nets[name]) { if (net.family === 'IPv4' && !net.internal) { lanAddresses.push(net.address); } } } console.log(''); console.log(' FS25 Web Dashboard running:'); console.log(' http://localhost:' + PORT); for (const addr of lanAddresses) { console.log(' http://' + addr + ':' + PORT + ' (other devices on your network, e.g. phone/tablet)'); } console.log(''); });