-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
64 lines (55 loc) · 2.22 KB
/
server.js
File metadata and controls
64 lines (55 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
require('dotenv').config();
console.log('🚀 STARTING BLOG API SERVER...');
console.log(`⏰ Server start time: ${new Date().toISOString()}`);
console.log(`🌐 Environment: ${process.env.NODE_ENV || 'development'}`);
try {
const app = require('./src/app');
const PORT = process.env.PORT || 5000;
const server = app.listen(PORT, () => {
console.log('\n=========================================');
console.log('✅ BLOG API SERVER STARTED SUCCESSFULLY!');
console.log(`📍 Port: ${PORT}`);
console.log(`🔗 Health Check: http://localhost:${PORT}/health`);
console.log(`📚 API Docs: http://localhost:${PORT}/api`);
console.log('=========================================\n');
});
// Enhanced server event listeners
server.on('error', (error) => {
console.error('❌ SERVER STARTUP ERROR:', error.message);
if (error.code === 'EADDRINUSE') {
console.error(`💡 Port ${PORT} is already in use. Try a different port.`);
}
process.exit(1);
});
// Graceful shutdown handling
process.on('SIGINT', () => {
console.log('\n🔻 Received SIGINT signal. Shutting down gracefully...');
server.close(() => {
console.log('✅ Server closed successfully.');
process.exit(0);
});
});
process.on('SIGTERM', () => {
console.log('\n🔻 Received SIGTERM signal. Shutting down gracefully...');
server.close(() => {
console.log('✅ Server closed successfully.');
process.exit(0);
});
});
// Uncaught exception handling
process.on('uncaughtException', (error) => {
console.error('❌ UNCAUGHT EXCEPTION:', error.message);
console.error('🔍 Stack trace:', error.stack);
process.exit(1);
});
process.on('unhandledRejection', (reason, promise) => {
console.error('❌ UNHANDLED PROMISE REJECTION:', reason);
console.error('🔍 At promise:', promise);
process.exit(1);
});
} catch (error) {
console.error('❌ CRITICAL SERVER STARTUP FAILURE:');
console.error('🔍 Error details:', error.message);
console.error('📋 Stack trace:', error.stack);
process.exit(1);
}