我有一个Angular应用程序,可以对“第三方” REST服务进行REST调用。将此服务视为www.someothercompany/restapi/MyObject/1
之类的东西。
当我在本地运行我的应用程序时(通过ng serve
),我的应用程序可以调用此服务。
当我在本地创建容器并在本地运行容器时,我的应用程序不与该服务一起使用。
所以我尝试调试它,在本地运行的容器上创建了一个终端窗口(我在Can't run Curl command inside my Docker Container中记录了我的“ add curl”)
curl -v --header "Accept: application/json" www.someothercompany/restapi/MyObject/1
此呼叫有效。
我的Angular代码如下:
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { map } from 'rxjs/operators';
import { environment } from '../../../../environments/environment';
import { MyObjectInfoRequest,MyObjectInfoResponse } from "./myObject-info.model";
@Injectable({ providedIn: 'root' })
export class MyObjectInfoService {
constructor(private http: HttpClient) {
}
getMyObject(myObjectRequest: MyObjectInfoRequest) {
let headers = new HttpHeaders();
headers.append('Accept','application/json');
return this.http.get<MyObjectInfoResponse>(
environment.myObjectInfoUrl+`?`,
{
headers:headers,
params: { _id: myObjectRequest.id }
})
.pipe(map(myObjectInfoResponse => {
return myObjectInfoResponse;
}));
}
}
http.get
看起来像我的curl
命令。
有人看到我做错的任何事情吗?
APPEND:
我的Dockerfile(主要由以下示例构成:How to create a Docker container of an AngularJS app?)
FROM node:latest as my-thing-builder
LABEL author="ME"
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm install
COPY . .
RUN npm run build -- --configuration=dev
FROM nginx:alpine
#RUN apk add --no-cache curl
# Install prerequisites
#RUN apt-get update && apt-get install -y curl
VOLUME /tmp/cache/nginx
# support running as arbitrary user which belogs to the root group
RUN chmod -R 777 /var/cache/nginx /var/run /var/log/nginx /var/cache/nginx/
# users are not allowed to listen on priviliged ports
RUN sed -i.bak 's/listen\(.*\)80;/listen 8081;/' /etc/nginx/conf.d/default.conf
EXPOSE 8081
# comment user directive as master process is run as user in OpenShift anyhow
RUN sed -i.bak 's/^user/#user/' /etc/nginx/nginx.conf
COPY --from=my-thing-builder /app/dist /usr/share/nginx/html
COPY ./scaffolding/nginx/nginx.dev /etc/nginx/conf.d/default.conf
CMD ["nginx", "-g","daemon off;"]
#USER 1001
# docker build -t my-thing:dev -f ./scaffolding/docker/my.dockerfile .
# docker run -d -p 8899:8080 my-thing:dev
# the docker run above will allow local http://localhost:8899/
APPEND
好吧...。我确实遇到了一些基本错误。
我的修订版。
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
import { map, catchError } from 'rxjs/operators';
import { Observable, throwError } from 'rxjs';
import { environment } from '../../../../environments/environment';
import { MyObjectInfoRequest,MyObjectInfoResponse } from "./myObject-info.model";
@Injectable({ providedIn: 'root' })
export class MyObjectInfoService {
constructor(private http: HttpClient) {
}
getMyObject(myObjectRequest: MyObjectInfoRequest) {
let headers = new HttpHeaders();
headers.append('Accept','application/json+fhir');
return this.http.get<MyObjectInfoResponse>(
environment.myObjectInfoUrl+`?`,
{
withCredentials: true,
headers:headers,
params: { _id: myObjectRequest.id }
})
.pipe(map(myObjectInfoResponse => {
return myObjectInfoResponse;
})
,
catchError(err => {
//console.log(err);
//alert(err);
this.handleError(err);
return throwError(err)
})
);
}
private handleError(error: HttpErrorResponse) {
if (error.error instanceof ErrorEvent) {
// A client-side or network error occurred. Handle it accordingly.
//alert('ErrorEvent ' + error.error.message);
console.error('An error occurred:', error.error.message);
} else {
// The backend returned an unsuccessful response code.
// The response body may contain clues as to what went wrong,
//alert('Else ' + error.status + ':::' + error.error);
console.error(
`Backend returned code ${error.status}, ` +
`body was: ${error.error}`);
}
// return an observable with a user-facing error message
return throwError(
'Something bad happened; please try again later.');
};
}
==========
啊哈!
502
错误的网关
nginx / 1.15.18
ERROR Object { headers: Object, status: 502, statusText: "Bad Gateway", url: "http://localhost:8899/restapi/MyObject/1", ok: false, name: "HttpErrorResponse", message: "Http failure response for http://localhost:8899/restapi/MyObject/1", error: "<html> <head><title>502 Bad Gateway…" }
答案 0 :(得分:0)
好吧,通过评论,我能够添加更好的errorHandling(这是一个片刻的时刻,但是我是AngularJS的新手,所以请耐心等待)
虽然我有一个用于反向代理设置的条目,但是没有参与。
因此需要对nginx配置进行一些调整
类似这样的东西:(原始)
location /restapi {
proxy_pass https://www.someothercompany;
}
分辨率(在同事的帮助下)更好的是https设置:
location /restapi {
proxy_ssl_server_name on;
proxy_pass https://www.someothercompany;
proxy_ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
proxy_ssl_ciphers HIGH:!aNULL:!MD5;
proxy_ssl_session_reuse on;
}
但是它的DID行为不同于本地运行和在容器中运行。当然,哪一个nginx是两者之间的“差异”。
这是我最初没有报告过的奇怪警告。它在部署到Open-Shift的容器中工作,但在本地docker-run中不工作。做什么?
我有点傻。
但是同时,我不会删除这个问题,因为我认为将来是否可以帮助某个人。