admin管理员组

文章数量:1122846

I'm converting my CSR Angular app to SSR using .NET 6. Now the .NET backend is having trouble serving the files for the frontend. All the files build and are correctly placed in the proper directories. See snippets for setup files below.

angular.json:

"server": {
      "builder": "@angular-devkit/build-angular:server",
      "options": {
        "outputPath": "dist/my-app/server",
        "main": "src/main.server.ts",
        "tsConfig": "tsconfig.server.json"
      },
      "configurations": {
       ...
      }
    },

        "serve-ssr": {
      "builder": "@angular-devkit/build-angular:ssr-dev-server",
      "options": {
        "host": "localhost",
        "port": 44492,
        "ssl": true,
        "sslCert": "%APPDATA%\\ASP.NET\\https\\%npm_package_name%.pem",
        "sslKey": "%APPDATA%\\ASP.NET\\https\\%npm_package_name%.key",
        "proxyConfig": "proxy.conf.js"
      },
      "configurations": {
        "development": {
          "browserTarget": "my-app:build:development",
          "serverTarget": "my-app:server:development"
        },
        "production": {
          "browserTarget": "my-app:build:production",
          "serverTarget": "my-app:server:production"
        }
      },
      "defaultConfiguration": "development"
    },

tsconfig.server.json:

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "./dist/my-app/server",
    "target": "es2022",
    "module": "es2022",
    "moduleResolution": "node",
    "types": ["node"],
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true
  },
  "files": [
    "src/main.server.ts"
  ],
  "angularCompilerOptions": {
    "enableIvy": true
  }
}

main.server.ts:

import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app/appponent';
import { serverAppConfig } from './app/app.config.server';

const bootstrap = () => bootstrapApplication(AppComponent, serverAppConfig);

export default bootstrap;

server.ts:

import 'zone.js/node';

import { APP_BASE_HREF } from '@angular/common';
import { CommonEngine } from '@angular/ssr/node';
import * as express from 'express';
import { existsSync } from 'node:fs';
import { join } from 'node:path';
import AppServerModule from './src/main.server';

// Define paths
const distFolder = join(process.cwd(), 'dist', 'my-app', 'browser');
const serverFolder = join(process.cwd(), 'dist', 'my-app', 'server');
const indexHtml = existsSync(join(distFolder, 'index.original.html'))
  ? join(distFolder, 'index.original.html')
  : join(distFolder, 'index.html');

// Initialize CommonEngine for SSR
const commonEngine = new CommonEngine();

export function app(): express.Express {
  const server = express();

  server.set('view engine', 'html');
  server.set('views', distFolder);

  // 

本文标签: cHow do I get Angular 19 SSR to work with NET 6Stack Overflow