|
1 | | -// IMMEDIATE STARTUP LOGGING - Write to stderr immediately to ensure iisnode captures it |
2 | | -// Use process.stderr.write for immediate output that iisnode can capture |
3 | | -process.stderr.write("=".repeat(50) + "\n"); |
4 | | -process.stderr.write("🚀 MayaCode Backend - Starting...\n"); |
5 | | -process.stderr.write("=".repeat(50) + "\n"); |
6 | | -process.stderr.write(`📋 Node.js Version: ${process.version}\n`); |
7 | | -process.stderr.write(`📋 Process PID: ${process.pid}\n`); |
8 | | -process.stderr.write(`📋 Working Directory: ${process.cwd()}\n`); |
9 | | -process.stderr.write(`📋 __dirname: ${__dirname}\n`); |
10 | | -process.stderr.write("=".repeat(50) + "\n"); |
| 1 | +// CRITICAL: Wrap entire file execution to catch ANY errors |
| 2 | +// This ensures we can log errors even if something fails before error handlers are set up |
| 3 | +(function() { |
| 4 | + try { |
| 5 | + // IMMEDIATE STARTUP LOGGING - Write to stderr immediately to ensure iisnode captures it |
| 6 | + // Use process.stderr.write for immediate output that iisnode can capture |
| 7 | + process.stderr.write("==================================================\n"); |
| 8 | + process.stderr.write("🚀 MayaCode Backend - Starting...\n"); |
| 9 | + process.stderr.write("==================================================\n"); |
| 10 | + process.stderr.write("📋 Node.js Version: " + process.version + "\n"); |
| 11 | + process.stderr.write("📋 Process PID: " + process.pid + "\n"); |
| 12 | + process.stderr.write("📋 Working Directory: " + process.cwd() + "\n"); |
| 13 | + process.stderr.write("📋 __dirname: " + __dirname + "\n"); |
| 14 | + process.stderr.write("==================================================\n"); |
| 15 | + |
| 16 | + // Also write to console |
| 17 | + console.log("=================================================="); |
| 18 | + console.log("🚀 MayaCode Backend - Starting..."); |
| 19 | + console.log("=================================================="); |
| 20 | + console.log("📋 Node.js Version:", process.version); |
| 21 | + console.log("📋 Process PID:", process.pid); |
| 22 | + console.log("📋 Working Directory:", process.cwd()); |
| 23 | + console.log("📋 __dirname:", __dirname); |
| 24 | + console.log("=================================================="); |
| 25 | + } catch (e) { |
| 26 | + // Last resort - try to write error to a file or use basic console |
| 27 | + try { |
| 28 | + const fs = require("fs"); |
| 29 | + const path = require("path"); |
| 30 | + const errorLogPath = path.join(__dirname, "../startup-error.log"); |
| 31 | + fs.writeFileSync(errorLogPath, "Failed to write startup logs: " + e.toString() + "\n" + e.stack); |
| 32 | + } catch (fileError) { |
| 33 | + // If even file writing fails, we're in deep trouble |
| 34 | + // This should never happen, but it's a safety net |
| 35 | + } |
| 36 | + } |
| 37 | +})(); |
11 | 38 |
|
12 | 39 | // Add error handling for missing dependencies |
13 | 40 | // Check if node_modules exists (warn but don't exit - actual requires will fail if missing) |
@@ -142,7 +169,20 @@ try { |
142 | 169 |
|
143 | 170 | // For iisnode, PORT is automatically set by Azure/IIS via environment variable |
144 | 171 | // Use default 8000 only for local development (should never happen in Azure) |
145 | | -const PORT = process.env.PORT || process.env.IISNODE_HTTP_PORT || 8000; |
| 172 | +let PORT = process.env.PORT || process.env.IISNODE_HTTP_PORT || 8000; |
| 173 | + |
| 174 | +// Validate PORT is a number |
| 175 | +PORT = parseInt(PORT, 10); |
| 176 | +if (isNaN(PORT) || PORT <= 0 || PORT > 65535) { |
| 177 | + process.stderr.write("❌ ERROR: Invalid PORT value: " + (process.env.PORT || process.env.IISNODE_HTTP_PORT || "8000") + "\n"); |
| 178 | + process.stderr.write("PORT must be a number between 1 and 65535\n"); |
| 179 | + console.error("❌ ERROR: Invalid PORT value:", process.env.PORT || process.env.IISNODE_HTTP_PORT || "8000"); |
| 180 | + console.error("PORT must be a number between 1 and 65535"); |
| 181 | + process.exit(1); |
| 182 | +} |
| 183 | + |
| 184 | +process.stderr.write("📋 Using PORT: " + PORT + "\n"); |
| 185 | +console.log("📋 Using PORT:", PORT); |
146 | 186 |
|
147 | 187 | // Log startup information for debugging |
148 | 188 | // Use both stderr and console for maximum visibility |
|
0 commit comments