【发布时间】:2021-05-22 05:32:18
【问题描述】:
我想阅读 StackOverflow 作业的 RSS 提要,我尝试添加“Access-Control-Allow-Headers”和代理实现,但它出错了。请帮我实现这个[已解决,请参阅下面的答案]
API 网址:https://stackoverflow.com/jobs/feed
private proxyPrefix = '/api';
private hostName = 'stackoverflow.com';
private location='sydney'
private query = `location=${this.location}`;
private path = 'jobs/feed';
private stackOverFlowJobsRssFeedUrl = `${this.hostName}/${this.path}?${this.query}`;
constructor(private http: HttpClient) { }
private getData$(url: string): Observable<any> {
const requestOptions = { headers: new HttpHeaders({ 'Access-Control-Allow-Headers': '*' }) };
return this.http.get<any>(url, requestOptions);
}
private get stackOverflowJobs$(): Observable<any> {
const proxyURL = `${this.proxyPrefix}/${this.path}?${this.query}`;
return fromFetch(proxyURL, {
selector: response => response.json()
});
}
ngOnInit(): void {
// http get
this.getData$(this.stackOverFlowJobsRssFeedUrl)
.subscribe((x) => {
console.log(x);
});
// rxJs fetch using proxy
this.stackOverflowJobs$.subscribe({
next: result => console.log('jobs:',result),
complete: () => console.log('done')
});
}
proxy.config
{
"/api/jobs": {
"target": {
"host": "https://stackoverflow.com",
"protocol": "https:",
"port": 443
},
"secure": false,
"changeOrigin": true,
"logLevel": "info"
}
}
错误
[HPM] Error occurred while trying to proxy request /api/jobs/feed?location=sydney from localhost:4200 to https://stackoverflow.com (ENOTFOUND) (https://nodejs.org/api/errors.html#errors_common_system_errors)
【问题讨论】: