Skip to content

Commit 16c4888

Browse files
feat: add clear log for occupied port at startup
1 parent aac85bb commit 16c4888

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ Thanks to: @dathbe.
1717
1818
### Added
1919

20-
- Added configuration option for `User-Agent`, used by calendar & news module (#3255)
21-
- [linter] Added prettier plugin for nunjuck templates (#3887)
20+
- Add configuration option for `User-Agent`, used by calendar & news module (#3255)
21+
- [linter] Add prettier plugin for nunjuck templates (#3887)
22+
- [core] Add clear log for occupied port at startup (#3890)
2223

2324
### Changed
2425

js/server.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,29 @@ function Server (config) {
5555
});
5656

5757
Log.log(`Starting server on port ${port} ... `);
58+
59+
// Add explicit error handling BEFORE calling listen so we can give user-friendly feedback
60+
server.once("error", (err) => {
61+
if (err && err.code === "EADDRINUSE") {
62+
const bindAddr = config.address || "localhost";
63+
const portInUseMessage = [
64+
"",
65+
"────────────────────────────────────────────────────────────────",
66+
` PORT IN USE: ${bindAddr}:${port}`,
67+
"",
68+
" Another process (most likely another MagicMirror instance)",
69+
" is already using this port.",
70+
"",
71+
" Stop the other process (free the port) or use a different port.",
72+
"────────────────────────────────────────────────────────────────"
73+
].join("\n");
74+
Log.error(portInUseMessage);
75+
return;
76+
}
77+
78+
Log.error("Failed to start server:", err);
79+
});
80+
5881
server.listen(port, config.address || "localhost");
5982

6083
if (config.ipWhitelist instanceof Array && config.ipWhitelist.length === 0) {

0 commit comments

Comments
 (0)