From 7ce96234672d28b3b46e22346bd8a5fde7e13396 Mon Sep 17 00:00:00 2001 From: greg Date: Sun, 27 Apr 2025 21:46:22 +0200 Subject: [PATCH] debug 4 --- my-favorites-app/Procfile | 2 +- my-favorites-app/nixpacks.toml | 5 +- my-favorites-app/package.json | 2 +- my-favorites-app/server.js | 111 ++++++++++++++++----------------- my-favorites-app/start.sh | 4 ++ 5 files changed, 63 insertions(+), 61 deletions(-) create mode 100644 my-favorites-app/start.sh diff --git a/my-favorites-app/Procfile b/my-favorites-app/Procfile index 063b78f..489b270 100644 --- a/my-favorites-app/Procfile +++ b/my-favorites-app/Procfile @@ -1 +1 @@ -web: npm start +web: node server.js diff --git a/my-favorites-app/nixpacks.toml b/my-favorites-app/nixpacks.toml index d287d34..e4fa070 100644 --- a/my-favorites-app/nixpacks.toml +++ b/my-favorites-app/nixpacks.toml @@ -1,5 +1,5 @@ [phases.setup] -nixPkgs = ["nodejs", "nodejs.pkgs.pnpm"] +nixPkgs = ["nodejs"] [phases.install] cmds = ["npm ci"] @@ -8,9 +8,8 @@ cmds = ["npm ci"] cmds = ["npm run build"] [start] -cmd = "npm start" +cmd = "node server.js" [variables] NODE_ENV = "production" PORT = "3000" -HOST = "0.0.0.0" diff --git a/my-favorites-app/package.json b/my-favorites-app/package.json index 1274c21..ffd58b3 100644 --- a/my-favorites-app/package.json +++ b/my-favorites-app/package.json @@ -5,7 +5,7 @@ "scripts": { "dev": "next dev", "build": "next build", - "start": "NODE_ENV=production node server.js", + "start": "node server.js", "lint": "next lint" }, "dependencies": { diff --git a/my-favorites-app/server.js b/my-favorites-app/server.js index 3f6a140..3ec23b7 100644 --- a/my-favorites-app/server.js +++ b/my-favorites-app/server.js @@ -1,66 +1,65 @@ -// Simple Express server for more reliable deployment -const express = require('express'); -const next = require('next'); +/** + * Extremely simplified server for Next.js + */ + +// Basic HTTP server +const http = require('http'); +const fs = require('fs'); const path = require('path'); -// Set up environment variables with defaults -const dev = process.env.NODE_ENV !== 'production'; -const port = parseInt(process.env.PORT || '3000', 10); +// Port configuration +const PORT = process.env.PORT || 3000; -// Create the Next.js app instance with minimal configuration -const app = next({ dev }); -const handle = app.getRequestHandler(); +// Simple static file server +const server = http.createServer((req, res) => { + console.log(`Request received: ${req.method} ${req.url}`); + + // Health check endpoint + if (req.url === '/health' || req.url === '/api/health') { + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ status: 'ok', timestamp: new Date().toISOString() })); + return; + } + + // Serve static HTML as fallback + const indexPath = path.join(__dirname, 'src', 'app', 'index.html'); + fs.readFile(indexPath, (err, content) => { + if (err) { + res.writeHead(500); + res.end('Error loading index.html'); + console.error('Error serving index.html:', err); + return; + } + + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.end(content); + }); +}); -// Log memory usage for debugging -const logMemoryUsage = () => { - const memoryUsage = process.memoryUsage(); - console.log('Memory usage:'); - console.log(` RSS: ${Math.round(memoryUsage.rss / 1024 / 1024)} MB`); - console.log(` Heap total: ${Math.round(memoryUsage.heapTotal / 1024 / 1024)} MB`); - console.log(` Heap used: ${Math.round(memoryUsage.heapUsed / 1024 / 1024)} MB`); -}; +// Start the server +server.listen(PORT, () => { + console.log(`Server running on port ${PORT}`); + console.log(`Environment: ${process.env.NODE_ENV || 'development'}`); +}); + +// Handle errors +server.on('error', (err) => { + console.error('Server error:', err); +}); + +// Handle process termination +process.on('SIGTERM', () => { + console.log('SIGTERM received, shutting down gracefully'); + server.close(() => { + console.log('Server closed'); + process.exit(0); + }); +}); -// Handle uncaught exceptions process.on('uncaughtException', (err) => { console.error('Uncaught exception:', err); - logMemoryUsage(); }); -process.on('unhandledRejection', (reason, promise) => { - console.error('Unhandled Rejection at:', promise, 'reason:', reason); - logMemoryUsage(); +process.on('unhandledRejection', (reason) => { + console.error('Unhandled rejection:', reason); }); - -// Simple health check response -const healthCheck = (req, res) => { - res.status(200).json({ status: 'ok', timestamp: new Date().toISOString() }); -}; - -app.prepare() - .then(() => { - const server = express(); - - // Add health check endpoint - server.get('/health', healthCheck); - server.get('/api/health', healthCheck); - - // Serve static files from the public directory - server.use(express.static(path.join(__dirname, 'public'))); - - // Let Next.js handle all other routes - server.all('*', (req, res) => { - return handle(req, res); - }); - - // Start the server - server.listen(port, '0.0.0.0', (err) => { - if (err) throw err; - console.log(`> Ready on port ${port} - env ${process.env.NODE_ENV || 'development'}`); - logMemoryUsage(); - }); - }) - .catch((ex) => { - console.error('An error occurred starting the app:', ex); - logMemoryUsage(); - process.exit(1); - }); diff --git a/my-favorites-app/start.sh b/my-favorites-app/start.sh new file mode 100644 index 0000000..6cb4951 --- /dev/null +++ b/my-favorites-app/start.sh @@ -0,0 +1,4 @@ +#!/bin/bash +# Simple startup script to ensure proper environment +export NODE_ENV=production +node server.js