67 lines
2.0 KiB
JavaScript
67 lines
2.0 KiB
JavaScript
// Simple Express server for more reliable deployment
|
|
const express = require('express');
|
|
const next = require('next');
|
|
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);
|
|
|
|
// Create the Next.js app instance with minimal configuration
|
|
const app = next({ dev });
|
|
const handle = app.getRequestHandler();
|
|
|
|
// 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`);
|
|
};
|
|
|
|
// 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();
|
|
});
|
|
|
|
// 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);
|
|
});
|