const { createServer } = require('http'); const { parse } = require('url'); const next = require('next'); const dev = process.env.NODE_ENV !== 'production'; const hostname = process.env.HOSTNAME || '0.0.0.0'; const port = parseInt(process.env.PORT || '3000', 10); // Prepare the Next.js app const app = next({ dev, hostname, port }); const handle = app.getRequestHandler(); app.prepare().then(() => { console.log(`Next.js App is preparing to start...`); createServer(async (req, res) => { try { // Parse the URL const parsedUrl = parse(req.url, true); // Let Next.js handle the request await handle(req, res, parsedUrl); } catch (err) { console.error('Error occurred handling request:', err); res.statusCode = 500; res.end('Internal Server Error'); } }) .once('error', (err) => { console.error('Server error:', err); process.exit(1); }) .listen(port, hostname, () => { console.log(`> Ready on http://${hostname}:${port}`); }); }).catch((err) => { console.error('Next.js app preparation failed:', err); process.exit(1); });