// IMPORTANT: DOM polyfill must be imported FIRST, before any browser-dependent libraries import './src/ssr-dom-polyfill'; import { APP_BASE_HREF } from '@angular/common'; import { AngularNodeAppEngine, createNodeRequestHandler, writeResponseToNodeResponse } from '@angular/ssr/node'; import { ɵsetAngularAppEngineManifest as setAngularAppEngineManifest } from '@angular/ssr'; import express from 'express'; import { fileURLToPath } from 'node:url'; import { dirname, join, resolve } from 'node:path'; // The Express app is exported so that it can be used by serverless Functions. export async function app(): Promise { const server = express(); const serverDistFolder = dirname(fileURLToPath(import.meta.url)); const browserDistFolder = resolve(serverDistFolder, '../browser'); const indexHtml = join(serverDistFolder, 'index.server.html'); // Explicitly load and set the Angular app engine manifest // This is required for environments where the manifest is not auto-loaded const manifestPath = join(serverDistFolder, 'angular-app-engine-manifest.mjs'); const manifest = await import(manifestPath); setAngularAppEngineManifest(manifest.default); const angularApp = new AngularNodeAppEngine(); server.set('view engine', 'html'); server.set('views', browserDistFolder); // Sitemap XML endpoints - MUST be before static files middleware server.get('/sitemap.xml', async (req, res) => { try { const sitemapIndexXml = ` https://www.bizmatch.net/sitemap-static.xml ${new Date().toISOString().split('T')[0]} `; res.header('Content-Type', 'application/xml; charset=utf-8'); res.send(sitemapIndexXml); } catch (error) { console.error('[SSR] Error generating sitemap index:', error); res.status(500).send('Error generating sitemap'); } }); server.get('/sitemap-static.xml', async (req, res) => { try { const sitemapXml = ` https://www.bizmatch.net/ daily 1.0 https://www.bizmatch.net/home daily 1.0 https://www.bizmatch.net/businessListings daily 0.9 https://www.bizmatch.net/commercialPropertyListings daily 0.9 https://www.bizmatch.net/brokerListings daily 0.9 https://www.bizmatch.net/terms-of-use monthly 0.5 https://www.bizmatch.net/privacy-statement monthly 0.5 `; res.header('Content-Type', 'application/xml; charset=utf-8'); res.send(sitemapXml); } catch (error) { console.error('[SSR] Error generating static sitemap:', error); res.status(500).send('Error generating sitemap'); } }); // Example Express Rest API endpoints // server.get('/api/**', (req, res) => { }); // Serve static files from /browser server.get('*.*', express.static(browserDistFolder, { maxAge: '1y' })); // All regular routes use the Angular engine server.get('*', async (req, res, next) => { console.log(`[SSR] Handling request: ${req.method} ${req.url}`); // Cache SSR-rendered pages at CDN level res.setHeader('Cache-Control', 'public, s-maxage=300, stale-while-revalidate=600'); try { const response = await angularApp.handle(req); if (response) { console.log(`[SSR] Response received for ${req.url}, status: ${response.status}`); writeResponseToNodeResponse(response, res); } else { console.log(`[SSR] No response for ${req.url} - Angular engine returned null`); console.log(`[SSR] This usually means the route couldn't be rendered. Check for: 1. Browser API usage in components 2. Missing platform checks 3. Errors during component initialization`); res.sendStatus(404); } } catch (err) { console.error(`[SSR] Error handling ${req.url}:`, err); console.error(`[SSR] Stack trace:`, err.stack); next(err); } }); return server; } // Global error handlers for debugging process.on('unhandledRejection', (reason, promise) => { console.error('[SSR] Unhandled Rejection at:', promise, 'reason:', reason); }); process.on('uncaughtException', (error) => { console.error('[SSR] Uncaught Exception:', error); console.error('[SSR] Stack:', error.stack); }); async function run(): Promise { const port = process.env['PORT'] || 4200; // Start up the Node server const server = await app(); server.listen(port, () => { console.log(`Node Express server listening on http://localhost:${port}`); }); } run();