This commit is contained in:
greg 2025-04-27 21:46:22 +02:00
parent f3e87f2b9c
commit 7ce9623467
5 changed files with 63 additions and 61 deletions

View File

@ -1 +1 @@
web: npm start web: node server.js

View File

@ -1,5 +1,5 @@
[phases.setup] [phases.setup]
nixPkgs = ["nodejs", "nodejs.pkgs.pnpm"] nixPkgs = ["nodejs"]
[phases.install] [phases.install]
cmds = ["npm ci"] cmds = ["npm ci"]
@ -8,9 +8,8 @@ cmds = ["npm ci"]
cmds = ["npm run build"] cmds = ["npm run build"]
[start] [start]
cmd = "npm start" cmd = "node server.js"
[variables] [variables]
NODE_ENV = "production" NODE_ENV = "production"
PORT = "3000" PORT = "3000"
HOST = "0.0.0.0"

View File

@ -5,7 +5,7 @@
"scripts": { "scripts": {
"dev": "next dev", "dev": "next dev",
"build": "next build", "build": "next build",
"start": "NODE_ENV=production node server.js", "start": "node server.js",
"lint": "next lint" "lint": "next lint"
}, },
"dependencies": { "dependencies": {

View File

@ -1,66 +1,65 @@
// Simple Express server for more reliable deployment /**
const express = require('express'); * Extremely simplified server for Next.js
const next = require('next'); */
// Basic HTTP server
const http = require('http');
const fs = require('fs');
const path = require('path'); const path = require('path');
// Set up environment variables with defaults // Port configuration
const dev = process.env.NODE_ENV !== 'production'; const PORT = process.env.PORT || 3000;
const port = parseInt(process.env.PORT || '3000', 10);
// Create the Next.js app instance with minimal configuration // Simple static file server
const app = next({ dev }); const server = http.createServer((req, res) => {
const handle = app.getRequestHandler(); console.log(`Request received: ${req.method} ${req.url}`);
// Log memory usage for debugging // Health check endpoint
const logMemoryUsage = () => { if (req.url === '/health' || req.url === '/api/health') {
const memoryUsage = process.memoryUsage(); res.writeHead(200, { 'Content-Type': 'application/json' });
console.log('Memory usage:'); res.end(JSON.stringify({ status: 'ok', timestamp: new Date().toISOString() }));
console.log(` RSS: ${Math.round(memoryUsage.rss / 1024 / 1024)} MB`); return;
console.log(` Heap total: ${Math.round(memoryUsage.heapTotal / 1024 / 1024)} MB`); }
console.log(` Heap used: ${Math.round(memoryUsage.heapUsed / 1024 / 1024)} MB`);
}; // 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);
});
});
// 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) => { process.on('uncaughtException', (err) => {
console.error('Uncaught exception:', err); console.error('Uncaught exception:', err);
logMemoryUsage();
}); });
process.on('unhandledRejection', (reason, promise) => { process.on('unhandledRejection', (reason) => {
console.error('Unhandled Rejection at:', promise, 'reason:', reason); console.error('Unhandled rejection:', reason);
logMemoryUsage();
}); });
// 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);
});

View File

@ -0,0 +1,4 @@
#!/bin/bash
# Simple startup script to ensure proper environment
export NODE_ENV=production
node server.js