@@ -33,7 +33,17 @@ app.use(express.json());
3333app . use ( '/api/posts' , PostRoutes ) ;
3434// Basic health check route
3535app . get ( '/' , ( req , res ) => {
36- res . status ( 200 ) . json ( { message : 'Backend server is running!' , status : 'OK' } ) ;
36+ res . status ( 200 ) . json ( {
37+ message : 'SkillSocket Backend is running!' ,
38+ status : 'OK' ,
39+ timestamp : new Date ( ) . toISOString ( ) ,
40+ port : process . env . PORT || 3000
41+ } ) ;
42+ } ) ;
43+
44+ // Simple ping endpoint for Render health checks
45+ app . get ( '/ping' , ( req , res ) => {
46+ res . status ( 200 ) . send ( 'pong' ) ;
3747} ) ;
3848
3949// Health check endpoint for Docker and monitoring
@@ -100,10 +110,23 @@ app.use('/api/events', eventRoutes);
100110// Export io for use in other modules
101111module . exports = { io } ;
102112
103- const MONGODB_URI = process . env . MONGODB_URI ;
104- mongoose . connect ( MONGODB_URI )
105- . then ( ( ) => console . log ( "MongoDB connected" ) )
106- . catch ( err => console . error ( "MongoDB connection error:" , err ) ) ;
113+ const MONGODB_URI = process . env . MONGODB_URI ;
114+
115+ console . log ( '📊 Connecting to MongoDB...' ) ;
116+ console . log ( '🔗 MongoDB URI exists:' , ! ! MONGODB_URI ) ;
117+
118+ mongoose . connect ( MONGODB_URI , {
119+ serverSelectionTimeoutMS : 5000 , // Timeout after 5s instead of 30s
120+ socketTimeoutMS : 45000 , // Close sockets after 45s of inactivity
121+ } )
122+ . then ( ( ) => {
123+ console . log ( "✅ MongoDB connected successfully" ) ;
124+ console . log ( "📊 Database ready state:" , mongoose . connection . readyState ) ;
125+ } )
126+ . catch ( err => {
127+ console . error ( "❌ MongoDB connection error:" , err . message ) ;
128+ process . exit ( 1 ) ;
129+ } ) ;
107130
108131const onlineUsers = new Map ( ) ;
109132
@@ -197,10 +220,12 @@ io.on('connection', (socket) => {
197220const DEFAULT_PORT = parseInt ( process . env . PORT , 10 ) || 3000 ;
198221
199222function startServer ( port = DEFAULT_PORT ) {
200- server . listen ( port , ( ) => {
223+ server . listen ( port , '0.0.0.0' , ( ) => {
201224 console . log ( `🚀 Server running on port ${ port } ` ) ;
225+ console . log ( `🌍 Server bound to 0.0.0.0:${ port } ` ) ;
202226 console . log ( `📋 Public upload endpoint: /api/user/upload-logo-public` ) ;
203227 console . log ( `🔒 Authenticated upload endpoint: /api/user/upload-logo` ) ;
228+ console . log ( `✅ Server ready to accept connections` ) ;
204229 } ) ;
205230
206231 // Handle server errors (for example, EADDRINUSE)
@@ -223,5 +248,39 @@ function startServer(port = DEFAULT_PORT) {
223248 } ) ;
224249}
225250
251+ // Graceful shutdown handlers
252+ process . on ( 'SIGTERM' , ( ) => {
253+ console . log ( '🔄 SIGTERM received, shutting down gracefully...' ) ;
254+ server . close ( ( ) => {
255+ console . log ( '✅ Server closed' ) ;
256+ mongoose . connection . close ( false , ( ) => {
257+ console . log ( '✅ MongoDB connection closed' ) ;
258+ process . exit ( 0 ) ;
259+ } ) ;
260+ } ) ;
261+ } ) ;
262+
263+ process . on ( 'SIGINT' , ( ) => {
264+ console . log ( '🔄 SIGINT received, shutting down gracefully...' ) ;
265+ server . close ( ( ) => {
266+ console . log ( '✅ Server closed' ) ;
267+ mongoose . connection . close ( false , ( ) => {
268+ console . log ( '✅ MongoDB connection closed' ) ;
269+ process . exit ( 0 ) ;
270+ } ) ;
271+ } ) ;
272+ } ) ;
273+
274+ // Handle uncaught exceptions
275+ process . on ( 'uncaughtException' , ( err ) => {
276+ console . error ( '💥 Uncaught Exception:' , err ) ;
277+ process . exit ( 1 ) ;
278+ } ) ;
279+
280+ process . on ( 'unhandledRejection' , ( reason , promise ) => {
281+ console . error ( '💥 Unhandled Rejection at:' , promise , 'reason:' , reason ) ;
282+ process . exit ( 1 ) ;
283+ } ) ;
284+
226285// Start the server
227286startServer ( ) ;
0 commit comments