* Server * @param {object} configObj The MM config full and redacted * @class
(configObj)
| 19 | * @class |
| 20 | */ |
| 21 | function Server (configObj) { |
| 22 | const config = configObj.fullConf; |
| 23 | const app = express(); |
| 24 | const port = process.env.MM_PORT || config.port; |
| 25 | const serverSockets = new Set(); |
| 26 | let server = null; |
| 27 | |
| 28 | /** |
| 29 | * Opens the server for incoming connections |
| 30 | * @returns {Promise} A promise that is resolved when the server listens to connections |
| 31 | */ |
| 32 | this.open = function () { |
| 33 | return new Promise((resolve) => { |
| 34 | if (config.useHttps) { |
| 35 | const options = { |
| 36 | key: fs.readFileSync(config.httpsPrivateKey), |
| 37 | cert: fs.readFileSync(config.httpsCertificate) |
| 38 | }; |
| 39 | server = https.Server(options, app); |
| 40 | } else { |
| 41 | server = http.Server(app); |
| 42 | } |
| 43 | const io = socketio(server, { |
| 44 | allowRequest: socketIpAccessControl(config.ipWhitelist), |
| 45 | cors: { |
| 46 | origin: /.*$/, |
| 47 | credentials: true |
| 48 | }, |
| 49 | allowEIO3: true, |
| 50 | pingInterval: 120000, // server → client ping every 2 mins |
| 51 | pingTimeout: 120000 // wait up to 2 mins for client pong |
| 52 | }); |
| 53 | |
| 54 | server.on("connection", (socket) => { |
| 55 | serverSockets.add(socket); |
| 56 | socket.on("close", () => { |
| 57 | serverSockets.delete(socket); |
| 58 | }); |
| 59 | }); |
| 60 | |
| 61 | Log.log(`Starting server on port ${port} ... `); |
| 62 | |
| 63 | // Add explicit error handling BEFORE calling listen so we can give user-friendly feedback |
| 64 | server.once("error", (err) => { |
| 65 | if (err && err.code === "EADDRINUSE") { |
| 66 | const bindAddr = config.address || "localhost"; |
| 67 | const portInUseMessage = [ |
| 68 | "", |
| 69 | "────────────────────────────────────────────────────────────────", |
| 70 | ` PORT IN USE: ${bindAddr}:${port}`, |
| 71 | "", |
| 72 | " Another process (most likely another MagicMirror instance)", |
| 73 | " is already using this port.", |
| 74 | "", |
| 75 | " Stop the other process (free the port) or use a different port.", |
| 76 | "────────────────────────────────────────────────────────────────" |
| 77 | ].join("\n"); |
| 78 | Log.error(portInUseMessage); |
nothing calls this directly
no test coverage detected