通过服务器上的 Http 请求的 URL 必须是绝对的

URLs requested via Http on the server must be absolute

我使用 angular2 创建了一个 angular 通用应用程序,我在其中请求 /category 服务。

this.hsService.getCategories(AppConstants.BASE_URL_GET_CATGORIES).subscribe(
  resp => {
    if (resp !== null) {
      console.log('response is not null');
    }else {
      console.log('response is null');
    }
  },
  err => {
    console.log('error');
    that.categories = that.Categories();
  }
);

但是我在错误下方遇到了这个错误。但是不明白为什么?

ERROR Error: URLs requested via Http on the server must be absolute. URL: /category at validateRequestUrl (D:\Myprojects\angular universal\ciel\node_modules\@angular\platform-server\bundles\platform-server.umd.js:99:15 at new ZoneMacroTaskConnection (D:\Myprojects\angular universal\ciel\node_modules\@angular\platform-server\bundles\platform-server.umd.js:226:9) at ZoneMacroTaskBackend.createConnection (D:\Myprojects\angular universal\ciel\node_modules\@angular\platform-server\bundles\platform-server.umd.js:262:16) at httpRequest (D:\Myprojects\angular universal\ciel\node_modules\@angular\http\bundles\http.umd.js:1833:20) at Http.request (D:\Myprojects\angular universal\ciel\node_modules\@angular\http\bundles\http.umd.js:1943:34) at Http.get (D:\Myprojects\angular universal\ciel\node_modules\@angular\http\bundles\http.umd.js:1957:21) at n.getCategories (D:\Myprojects\angular universal\ciel\dist-server\main.bundle.js:1:26301) at n.XV61.n.getCategories (D:\Myprojects\angular universal\ciel\dist-server\main.bundle.js:1:24428) at n.XV61.n.ngOnInit (D:\Myprojects\angular universal\ciel\dist-server\main.bundle.js:1:24346) at checkAndUpdateDirectiveInline (D:\Myprojects\angular universal\ciel\node_modules\@angular\core\bundles\core.umd.js:10875:19)

有人可以帮助我吗?

ERROR Error: URLs requested via Http on the server must be absolute.

看起来 AppConstants.BASE_URL_GET_CATGORIESundefined 或无效 http URL。

我认为你需要注入原点url来创建绝对路径

export function createTranslateLoader(http: Http, @Inject('AppConstants.BASE_URL_GET_CATGORIES') originUrl: string) {
  return new TranslateHttpLoader(http, originUrl);
}

在服务器端呈现中,任何 HTTP 调用都需要绝对 URL。

您可以

  1. 对 HTTP 请求使用绝对 URLs
  2. 注入来源 URL 并添加到基础 URL 服务器端

问题的答案中有多种解决方法可以执行选项 2

我个人建议配置一个注入令牌,为您提供服务器的来源,并使用 HTTP 拦截器将其添加到基础 URL:

添加 HTTP 拦截器class:

import { Injectable, Inject, Optional } from '@angular/core';
import { HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http';

@Injectable()
export class UniversalInterceptor implements HttpInterceptor {

  constructor(@Optional() @Inject('serverUrl') protected serverUrl: string) {}

  intercept(req: HttpRequest<any>, next: HttpHandler) {

    const serverReq = !this.serverUrl ? req : req.clone({
      url: `${this.serverUrl}${req.url}`
    });

    return next.handle(serverReq);

  }
}

将其添加到服务器端模块的提供程序数组:

providers: [
{
  provide: HTTP_INTERCEPTORS,
  useClass: UniversalInterceptor,
  multi: true
}

在您的服务器端配置中(在此示例中为 express),提供带有服务器来源的令牌 URL:

let protocol = 'http';
if (process.env.NODE_ENV == 'production') {
   protocol = 'https'
}

app.engine('html', (_, options, callback) => {
  let engine = ngExpressEngine({
    bootstrap: AppServerModuleNgFactory,
    providers: [
      provideModuleMap(LAZY_MODULE_MAP),
      {
        provide: 'serverUrl',
        useValue: `${protocol}://${options.req.get('host')}`
      }
    ]
  });

  engine(_, options, callback)
})