@@ -17,31 +17,33 @@ let child = null;
1717let restartTimer = null ;
1818let isShuttingDown = false ;
1919let isRestarting = false ;
20- let serverPort = null ;
20+ let serverConfig = null ;
2121const rootDir = path . join ( __dirname , ".." ) ;
2222
2323/**
24- * Get the server port from config
25- * @returns {number } The port number
24+ * Get the server configuration ( port and address)
25+ * @returns {{port: number, address: string} } The server config
2626 */
27- function getServerPort ( ) {
28- if ( serverPort ) return serverPort ;
27+ function getServerConfig ( ) {
28+ if ( serverConfig ) return serverConfig ;
2929
3030 try {
31- // Try to read the config file to get the port
3231 const configPath = getConfigFilePath ( ) ;
3332 delete require . cache [ require . resolve ( configPath ) ] ;
3433 const config = require ( configPath ) ;
35- serverPort = global . mmPort || config . port || 8080 ;
34+ serverConfig = {
35+ port : global . mmPort || config . port || 8080 ,
36+ address : config . address || "localhost"
37+ } ;
3638 } catch ( err ) {
37- serverPort = 8080 ;
39+ serverConfig = { port : 8080 , address : "localhost" } ;
3840 }
3941
40- return serverPort ;
42+ return serverConfig ;
4143}
4244
4345/**
44- * Check if a port is available
46+ * Check if a port is available on the configured address
4547 * @param {number } port The port to check
4648 * @returns {Promise<boolean> } True if port is available
4749 */
@@ -58,7 +60,9 @@ function isPortAvailable (port) {
5860 resolve ( true ) ;
5961 } ) ;
6062
61- server . listen ( port ) ;
63+ // Use the same address as the actual server will bind to
64+ const { address } = getServerConfig ( ) ;
65+ server . listen ( port , address ) ;
6266 } ) ;
6367}
6468
@@ -115,7 +119,7 @@ function startServer () {
115119 * Send reload notification to all connected clients
116120 */
117121function notifyClientsToReload ( ) {
118- const port = getServerPort ( ) ;
122+ const { port } = getServerConfig ( ) ;
119123 const options = {
120124 hostname : "localhost" ,
121125 port : port ,
@@ -151,7 +155,7 @@ async function restartServer (reason) {
151155 isRestarting = true ;
152156
153157 // Get the actual port being used
154- const port = getServerPort ( ) ;
158+ const { port } = getServerConfig ( ) ;
155159
156160 // Notify clients to reload before restart
157161 notifyClientsToReload ( ) ;
@@ -160,8 +164,8 @@ async function restartServer (reason) {
160164 child . once ( "exit" , async ( ) => {
161165 // Wait until port is actually available
162166 await waitForPort ( port ) ;
163- // Reset port cache in case config changed
164- serverPort = null ;
167+ // Reset config cache in case it changed
168+ serverConfig = null ;
165169 startServer ( ) ;
166170 } ) ;
167171
0 commit comments