npm run serve:ssr funktioniert und Hamburger Menu bug fix
This commit is contained in:
@@ -3,17 +3,24 @@ 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 function app(): express.Express {
|
||||
export async function app(): Promise<express.Express> {
|
||||
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');
|
||||
@@ -27,27 +34,46 @@ export function app(): express.Express {
|
||||
}));
|
||||
|
||||
// All regular routes use the Angular engine
|
||||
server.get('*', (req, res, next) => {
|
||||
angularApp
|
||||
.handle(req)
|
||||
.then((response) => {
|
||||
if (response) {
|
||||
writeResponseToNodeResponse(response, res);
|
||||
} else {
|
||||
res.sendStatus(404);
|
||||
}
|
||||
})
|
||||
.catch((err) => next(err));
|
||||
server.get('*', async (req, res, next) => {
|
||||
console.log(`[SSR] Handling request: ${req.method} ${req.url}`);
|
||||
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;
|
||||
}
|
||||
|
||||
function run(): void {
|
||||
// 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<void> {
|
||||
const port = process.env['PORT'] || 4200;
|
||||
|
||||
// Start up the Node server
|
||||
const server = app();
|
||||
const server = await app();
|
||||
server.listen(port, () => {
|
||||
console.log(`Node Express server listening on http://localhost:${port}`);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user