1

我有一个用 Angular 4 开发的 UI 应用程序。我还有一个服务超级登录和数据库沙发数据库。我正在从 Angular 4 应用程序向别名为 couchdb:5984 的沙发数据库发出请求,请求的状态为失败(失败)net:ERR_CONNECTION_REFUSED

回购:https ://github.com/janastu/iihs_couch_services

下面是我的 docker-compose.yml

version: '2'
volumes:
couchdb_data: {}
services:
# Build the container using the client Dockerfile
client:
build: ./client
  # This line maps the contents of the client folder into the container.
ports:
  - "80:80"
links:
  - superlogin:superlogin
  - couchdb:couchdb  
couchdb:
environment:
- COUCHDB_USER:somename
 - COUCHDB_PASSWORD:somepassword
volumes:
 - ./client:/usr/src/app

# Build the container using the superlogin Dockerfile
superlogin:
build: ./superlogin>    ports:
- "3000:3000"
# Link the client container so that Nginx will have access to it
links:
- couchdb:couchdb
couchdb:
environment:
- COUCHDB_USER:somename
- COUCHDB_PASSWORD:somepassword
image: klaemo/couchdb:1.6-couchperuser
ports:
- "5984:5984"
volumes:
- "couchdb_data:/usr/local/var/lib/couchdb"

所有容器都已启动并正在运行。如何从 Angular 4 应用程序的容器沙发数据库访问数据库?

执行 docker-compose up 后的屏幕截图

4

1 回答 1

1

我假设 Angular 应用程序是在浏览器中执行的,例如在开发上下文中使用ng serve和访问 as http://localhost:4200/,对吗?您的本地网络不知道 Docker 服务名称couchdb- 因此您应该创建请求来localhost:5984代替。

执行 XHR 的浏览器需要在 CouchDB 实例的 HTTP 响应中设置正确的 CORS 标头才能执行成功的请求,例如

Access-Control-Allow-Headers: Accept, Content-Type
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Origin: *

有关跨域资源共享 (CORS) 的信息,请参阅https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS和https://enable-cors.org/server_nginx.html nginx web 服务器中的示例配置。

如果直接查询 CouchDB,则需要启用和配置(自 CouchDB 1.3 起可用),请参阅http://docs.couchdb.org/en/stable/config/http.html#cross-origin-resource-sharinghttps ://wiki.apache.org/couchdb/CORS

CouchDB 的 local.ini 文件的基本配置示例

[httpd]
enable_cors = true

[cors]
origins = *

由于您的应用程序可能是稍后部署到某些不同环境的目标,因此您可能需要考虑使用 Angularenvironments设置使 Web 服务端点可配置 - 例如 fordevprodcontext。

于 2018-01-03T13:14:21.227 回答