From 0c28b129782e6f99da0a1136e04a3788ff04b60b Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Mon, 1 Dec 2025 08:43:31 +0100 Subject: [PATCH] Enhance HTTPS server setup and logging for Socket.IO - Added detailed logging for SSL certificate loading and server status checks. - Implemented error handling for port conflicts, providing guidance on resolving issues. - Introduced a verification step to confirm server activation after startup. - Improved fallback behavior for Socket.IO when SSL certificates are not found. --- backend/server.js | 44 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/backend/server.js b/backend/server.js index 3d3b05f..e206791 100644 --- a/backend/server.js +++ b/backend/server.js @@ -4,6 +4,7 @@ import { fileURLToPath } from 'url'; import { createServer } from 'http'; import https from 'https'; import fs from 'fs'; +import { exec } from 'child_process'; import sequelize from './database.js'; import cors from 'cors'; import { initializeSocketIO } from './services/socketService.js'; @@ -337,10 +338,12 @@ app.use((err, req, res, next) => { if (fs.existsSync(sslKeyPath) && fs.existsSync(sslCertPath)) { try { + console.log('📜 Lade SSL-Zertifikate...'); const httpsOptions = { key: fs.readFileSync(sslKeyPath), cert: fs.readFileSync(sslCertPath) }; + console.log('✅ SSL-Zertifikate erfolgreich geladen'); // Erstelle HTTPS-Server mit Express-App const httpsServer = https.createServer(httpsOptions, app); @@ -349,15 +352,18 @@ app.use((err, req, res, next) => { initializeSocketIO(httpsServer); socketIOInitialized = true; - httpsServer.listen(httpsPort, '0.0.0.0', () => { - console.log(`🚀 HTTPS-Server für Socket.IO läuft auf Port ${httpsPort}`); - console.log(` Socket.IO Endpoint: https://tt-tagebuch.de:${httpsPort}/socket.io/`); - }); - - // Error-Handling für HTTPS-Server + // Prüfe, ob Port bereits belegt ist httpsServer.on('error', (err) => { - console.error('❌ HTTPS-Server Error:', err.message); - console.error(' Code:', err.code); + if (err.code === 'EADDRINUSE') { + console.error(`❌ Port ${httpsPort} ist bereits belegt!`); + console.error(' → Möglicherweise läuft bereits ein anderer Server auf diesem Port'); + console.error(' → Prüfe mit: lsof -i :3051 oder netstat -tlnp | grep 3051'); + socketIOInitialized = false; + } else { + console.error('❌ HTTPS-Server Error:', err.message); + console.error(' Code:', err.code); + socketIOInitialized = false; + } }); httpsServer.on('clientError', (err, socket) => { @@ -366,14 +372,36 @@ app.use((err, req, res, next) => { socket.end('HTTP/1.1 400 Bad Request\r\n\r\n'); } }); + + // Starte HTTPS-Server + httpsServer.listen(httpsPort, '0.0.0.0', () => { + console.log(`🚀 HTTPS-Server für Socket.IO läuft auf Port ${httpsPort}`); + console.log(` Socket.IO Endpoint: https://tt-tagebuch.de:${httpsPort}/socket.io/`); + console.log(` Prüfe mit: lsof -i :${httpsPort} oder netstat -tlnp | grep ${httpsPort}`); + }); + + // Prüfe nach kurzer Verzögerung, ob Server wirklich läuft + setTimeout(() => { + if (socketIOInitialized) { + exec(`lsof -i :${httpsPort} || netstat -tlnp 2>/dev/null | grep :${httpsPort} || echo "Port nicht gefunden"`, (error, stdout) => { + if (stdout && !stdout.includes('Port nicht gefunden')) { + console.log(`✅ Port ${httpsPort} ist aktiv und erreichbar`); + } else { + console.warn(`⚠️ Port ${httpsPort} scheint nicht aktiv zu sein - prüfe Server-Logs`); + } + }); + } + }, 2000); } catch (err) { console.error('⚠️ HTTPS-Server konnte nicht gestartet werden:', err.message); console.error(' Stack:', err.stack); console.log(' → Socket.IO läuft auf HTTP-Server (nur für Entwicklung)'); + socketIOInitialized = false; } } else { console.log('ℹ️ SSL-Zertifikate nicht gefunden - Socket.IO läuft auf HTTP-Server (nur für Entwicklung)'); console.log(` Erwartete Pfade: ${sslKeyPath}, ${sslCertPath}`); + console.log(` Prüfe mit: ls -la ${sslKeyPath} ${sslCertPath}`); } // Fallback: Socket.IO auf HTTP-Server (wenn noch nicht initialisiert)