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

问题描述 投票:1回答:2

我创建了一个角度通用的应用程序与angular2我要求/类别服务。

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();
  }
);

但我错误地得到了这个错误。但是不明白为什么?

错误错误:服务器上通过Http请求的URL必须是绝对的。 URL:/,类别在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)在ZoneMacroTaskBackend.createConnection(D:\ Myprojects \ angular universal \ ciel \ node_modules \ @angular \ platform-server \在http://in :. (D:\ Myprojects \ angular universal \ ciel \ node_modules \ @angular \ http \ bundles \ http.umd.js:1943:34)在Http.get(D:\ Myprojects \ angular universal \ ciel \ node_modules \ @angular \在n.XV61.n的n.getCategories(D:\ Myprojects \ angular universal \ ciel \ dist-server \ main.bundle.js:1:26301)的http \ bundles \ http.umd.js:1957:21)在n.XV61.n.ngOnInit(D:\ Myproj)的getCategories(D:\ Myprojects \ angular universal \ ciel \ dist-server \ main.bundle.js:1:24428) ects \ angular universal \ ciel \ dist-server \ main.bundle.js:1:24346)checkAndUpdateDirectiveInline(D:\ Myprojects \ angular universal \ ciel \ node_modules \ @angular \ core \ bundles \ core.umd.js:10875 :19)

有谁能够帮我?

node.js angular angular-universal
2个回答
0
投票

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

看起来像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);
}

0
投票

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

你也可以

  1. 对HTTP请求使用绝对URL
  2. 注入原始URL并添加到基本URL服务器端

this问题的答案中,有多种解决方法可以做选项2。

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

添加HTTP拦截器类:

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数组:

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

在服务器端配置中(在此示例中表示),为令牌提供服务器的原始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)
})
© www.soinside.com 2019 - 2024. All rights reserved.