首页 > 解决方案 > Angular cli 代理配置不适用于本地主机?

问题描述

试图调用http://localhost:9001使用角度代理配置运行的nodejs localhost但是我无法调用端点,知道我在下面的代码中实现了什么错误吗?

服务.ts

import { Injectable } from '@angular/core';
import { HttpClient , HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';

@Injectable({ providedIn: 'root' })
export class AdminService {

  constructor(private http: HttpClient) {}

  private url = "/api/saveClients";

  saveClientJobs(data) {
    let options = this.createRequestOptions();
    console.log("Service", data);
    return this.http.post(this.url, data, {
      headers: options
    });
  }

  private createRequestOptions() {
    let headers = new HttpHeaders({
      "Content-Type": "application/json"
    });
    return headers;
  }
}

proxy.config.json

{
  "/api/*": {
    "target": "http://localhost:9001",
    "secure": false,
    "logLevel": "debug"
  }
}

包.json

"scripts": {
  "ng": "ng",
  "start": "ng serve --proxy-config proxy.config.json",
  "build": "ng build",
  "test": "ng test",
  "lint": "ng lint",
  "e2e": "ng e2e"
}

标签: node.jsangulartypescripthttp

解决方案


推荐阅读