我想创建一个 angular.io 应用程序,但其余 API 应从不同的服务器端口提供。
来自 localhost:4200 的 Angular 内容,来自 node express 服务器的数据在 localhost:3000 上独立启动。 但是,当我注入(inject)并使用“http”时,如何配置要使用的端口?
最佳答案
以某种方式获得“正确”URL 的解决方案无效。
事实证明,这会导致一种情况,即在其余服务器上将 http.put(...) 视为 OPTIONS 请求。
Angular $http is sending OPTIONS instead of PUT/POST
我找到了一种工作方式,通过从 Angular 服务器代理到其余服务器:
angular-cli server - how to proxy API requests to another server?
ng serve --proxy-config proxy.conf.json
proxy.conf.json:
{
"/api": {
"target": "http://localhost:3000",
"secure": false
}
}
关于angular - 在 Angular.io 中为 HTTP 配置端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42100109/