Skip to content

Commit 53da784

Browse files
committed
update
1 parent c944916 commit 53da784

2 files changed

Lines changed: 85 additions & 11 deletions

File tree

Backend/src/health-check.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Minimal health check script for iisnode
2+
// This file should be accessible via: https://your-app.azurewebsites.net/health-check.js
3+
// If this works but index.js doesn't, it's a problem with index.js or its dependencies
4+
5+
process.stderr.write("Health check script executed\n");
6+
process.stderr.write("Node version: " + process.version + "\n");
7+
process.stderr.write("Working directory: " + process.cwd() + "\n");
8+
9+
const http = require("http");
10+
11+
const server = http.createServer((req, res) => {
12+
process.stderr.write("Health check request received\n");
13+
res.writeHead(200, { "Content-Type": "application/json" });
14+
res.end(JSON.stringify({
15+
status: "healthy",
16+
nodeVersion: process.version,
17+
timestamp: new Date().toISOString(),
18+
message: "Health check endpoint is working"
19+
}));
20+
});
21+
22+
const PORT = process.env.PORT || 8000;
23+
process.stderr.write("Attempting to listen on port: " + PORT + "\n");
24+
25+
server.listen(PORT, () => {
26+
process.stderr.write("Health check server listening on port: " + PORT + "\n");
27+
console.log("Health check server is running");
28+
}).on('error', (error) => {
29+
process.stderr.write("Health check server error: " + error.message + "\n");
30+
console.error("Health check server error:", error);
31+
});
32+
33+
module.exports = server;
34+

Backend/src/index.js

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,40 @@
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+
})();
1138

1239
// Add error handling for missing dependencies
1340
// Check if node_modules exists (warn but don't exit - actual requires will fail if missing)
@@ -142,7 +169,20 @@ try {
142169

143170
// For iisnode, PORT is automatically set by Azure/IIS via environment variable
144171
// 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);
146186

147187
// Log startup information for debugging
148188
// Use both stderr and console for maximum visibility

0 commit comments

Comments
 (0)