@@ -91,9 +91,6 @@ mountServerlessFunction('/api/enumerators-stats', path.join(__dirname, '../api/e
9191mountServerlessFunction ( '/api/admin/sync-users' , path . join ( __dirname , '../api/admin/sync-users.js' ) ) ;
9292mountServerlessFunction ( '/api/admin/refresh-enumerator-stats' , path . join ( __dirname , '../api/admin/refresh-enumerator-stats.js' ) ) ;
9393
94- // Debug endpoints
95- mountServerlessFunction ( '/api/debug/survey-collections' , path . join ( __dirname , '../api/debug/survey-collections.js' ) ) ;
96-
9794console . log ( '\n✅ All endpoints mounted successfully!\n' ) ;
9895
9996// Health check endpoint
@@ -168,6 +165,35 @@ app.use((err, req, res, next) => {
168165 } ) ;
169166} ) ;
170167
168+ // Helper function to try starting server on a port
169+ function tryStartServer ( port , originalPort = port , maxAttempts = 10 ) {
170+ return new Promise ( ( resolve , reject ) => {
171+ const server = app . listen ( port , ( ) => {
172+ console . log ( `🚀 Development server running on port ${ port } ` ) ;
173+ console . log ( ` http://localhost:${ port } ` ) ;
174+ console . log ( `\nBackend ready! Start the frontend with: npm run dev:frontend\n` ) ;
175+ resolve ( server ) ;
176+ } ) ;
177+
178+ server . on ( 'error' , ( error ) => {
179+ if ( error . code === 'EADDRINUSE' ) {
180+ server . close ( ) ;
181+ if ( maxAttempts > 0 ) {
182+ console . log ( `Port ${ port } is in use, trying port ${ port + 1 } ...` ) ;
183+ tryStartServer ( port + 1 , originalPort , maxAttempts - 1 )
184+ . then ( resolve )
185+ . catch ( reject ) ;
186+ } else {
187+ reject ( new Error ( `Could not find an available port starting from ${ originalPort } ` ) ) ;
188+ }
189+ } else {
190+ server . close ( ) ;
191+ reject ( error ) ;
192+ }
193+ } ) ;
194+ } ) ;
195+ }
196+
171197// Start server with MongoDB connection validation
172198async function startServer ( ) {
173199 try {
@@ -176,16 +202,15 @@ async function startServer() {
176202 const { db } = await connectToDatabase ( ) ;
177203 console . log ( `✓ MongoDB connected: ${ db . databaseName } \n` ) ;
178204
179- app . listen ( PORT , ( ) => {
180- console . log ( `🚀 Development server running on port ${ PORT } ` ) ;
181- console . log ( ` http://localhost:${ PORT } ` ) ;
182- console . log ( `\nBackend ready! Start the frontend with: npm run dev:frontend\n` ) ;
183- } ) ;
205+ // Try to start server on the specified port, or find an available port
206+ await tryStartServer ( PORT ) ;
184207 } catch ( error ) {
185208 console . error ( '❌ Failed to start server:' , error . message ) ;
186- console . error ( '\nPlease check your MongoDB connection settings:' ) ;
187- console . error ( ' - MONGODB_VALIDATION_URI in .env file' ) ;
188- console . error ( ' - MONGODB_VALIDATION_DB in .env file' ) ;
209+ if ( error . message . includes ( 'MongoDB' ) || error . message . includes ( 'MONGODB' ) ) {
210+ console . error ( '\nPlease check your MongoDB connection settings:' ) ;
211+ console . error ( ' - MONGODB_VALIDATION_URI in .env file' ) ;
212+ console . error ( ' - MONGODB_VALIDATION_DB in .env file' ) ;
213+ }
189214 console . error ( `\nError details: ${ error . stack } ` ) ;
190215 process . exit ( 1 ) ;
191216 }
0 commit comments