Angular服务器渲染(SSR)测试问题

proxy.config.json: more

{
  "/api": {
    "target": "https://api.domain.com",
    "secure": false,
    "changeOrigin": true
  }
}

angular.json: proxyConfig

        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "configurations": {
            "development": {
              ...
              "proxyConfig": "proxy.config.json"
            }
          },
          ...
        },
        ...
        "serve-ssr": {
          "builder": "@nguniversal/builders:ssr-dev-server",
          "configurations": {
            "development": {
              ...
              "proxyConfig": "proxy.config.json"
            },
            ...
        },

app.server.module.ts: useAbsoluteUrl

import {
  INITIAL_CONFIG,
  PlatformConfig,
  ServerModule,
} from '@angular/platform-server';
...
const platformConfig: PlatformConfig = { useAbsoluteUrl: true };
...
@NgModule({
  imports: [AppModule, ServerModule],
  bootstrap: [AppComponent],
  providers: [
    ...
    { provide: INITIAL_CONFIG, useValue: platformConfig },
  ],

server.ts: api proxy

import { createProxyMiddleware, Options } from 'http-proxy-middleware';
...
  const proxyFile = join(process.cwd(), 'proxy.config.json');
  if (existsSync(proxyFile)) {
    const json = readFileSync(proxyFile).toString();
    const config: Record<string, Options> = JSON.parse(json);
    Object.keys(config).forEach((key) => {
      server.use(key, createProxyMiddleware(config[key]));
    });
  }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容