@@ -96,20 +96,35 @@ server.on('request', async (req, res) => {
9696
9797const port = process . argv [ 2 ] ? parseInt ( process . argv [ 2 ] , 10 ) : 49342 ;
9898
99- server . listen ( port ) ;
100-
101- server . on ( 'listening' , ( ) => {
102- const usedPort = ( server . address ( ) as AddressInfo ) . port ;
103- const serverUrl = `http://localhost:${ usedPort } ` ;
104- console . log ( serverUrl ) ;
105- console . log ( `Server is listening on: ${ serverUrl } ` ) ;
106- console . log ( "To launch a new fuel-core node and get its url, make a POST request to '/'." ) ;
107- console . log (
108- "To kill the node, make a POST request to '/cleanup/<url>' where <url> is the url of the node you want to kill."
109- ) ;
110- console . log ( 'All nodes will be killed when the server is closed.' ) ;
111- console . log ( 'You can close the server by sending a request to /close-server.' ) ;
112- } ) ;
99+ try {
100+ server . listen ( port ) ;
101+
102+ server . on ( 'error' , ( error : NodeJS . ErrnoException ) => {
103+ if ( error . code === 'EADDRINUSE' ) {
104+ console . log ( `Port ${ port } is already in use. Another server may be running.` ) ;
105+ process . exit ( 1 ) ;
106+ } else {
107+ console . error ( 'Server error:' , error ) ;
108+ process . exit ( 1 ) ;
109+ }
110+ } ) ;
111+
112+ server . on ( 'listening' , ( ) => {
113+ const usedPort = ( server . address ( ) as AddressInfo ) . port ;
114+ const serverUrl = `http://localhost:${ usedPort } ` ;
115+ console . log ( serverUrl ) ;
116+ console . log ( `Server is listening on: ${ serverUrl } ` ) ;
117+ console . log ( "To launch a new fuel-core node and get its url, make a POST request to '/'." ) ;
118+ console . log (
119+ "To kill the node, make a POST request to '/cleanup/<url>' where <url> is the url of the node you want to kill."
120+ ) ;
121+ console . log ( 'All nodes will be killed when the server is closed.' ) ;
122+ console . log ( 'You can close the server by sending a request to /close-server.' ) ;
123+ } ) ;
124+ } catch ( err ) {
125+ console . error ( 'Error starting server:' , err ) ;
126+ process . exit ( 1 ) ;
127+ }
113128
114129process . on ( 'exit' , closeServer ( 'exit' ) ) ;
115130process . on ( 'SIGINT' , closeServer ( 'SIGINT' ) ) ;
0 commit comments