无法与DockErized节点API通信dockerixed Elixir API
这是我的尝试,看看您是否可以帮助我解决我整整一周一直在尝试解决的问题。
上下文:
我的前端应用程序已连接到一个API,一个API在长生不老药中。一切都按预期工作。
Elixir API仅用于一个端点内置的节点内置的另一个API(我们无法在Elixir中进行编码,因此我们在JS中尝试了),
当Frontend向特定端点提供请求时(例如 /搜索),Elixir应该呼吁使用Elixir。该端点在节点中并返回一些数据。
一切都可以在本地完美。这两个图像都可以通信,并且它们之间共享数据。但是,当我转到登台环境时,当我从前端环境到相同端点的请求时,我总是会收到一个经济错误的错误。
两个Docker图像都连接到同一网络时。他们可以互相ping,卷发在它们之间起作用。但是,当我通过API时,无法获取预期数据。
docker-compose图像:节点API
version: '3'
services:
web:
container_name: node_api
build: .
ports:
- '8050:80'
expose:
- '8050'
networks:
- test
command: node index.js
networks:
test:
external: true
docker-compose图像:elixir api
version: "3"
services:
web:
build: .
depends_on:
- mariadb
ports:
- "8051:8011"
environment:
MIX_ENV: "dev"
DATABASE_URL: "ecto://root:${MYSQL_ROOT_PASSWORD}@mariadb/test"
MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD}"
LC_ALL: "en_US.UTF-8"
LANG: "en_US.UTF-8"
LANGUAGE: "en_US.UTF-8"
command: "mix ecto.create && mix phx.server"
user: "root"
links:
- mariadb
networks:
- test
networks:
test:
external: true
我尝试更改elixir api均以名称为单位(在这种情况下为node_api),通过IP,似乎没有任何作用。
这是两种服务在服务器上如何运行的图片(第一个是节点,第二个是elixir)
< img src =“ https://i.sstatic.net/wqfm4.png” alt =“在此处输入图像说明”>
这是Elixir API端点的代码
url = "#{url}/search"
body =
Jason.encode!(%{
from_date: from_date,
search: search,
to_date: to_date,
page_size: page_size,
})
headers = [{"Content-type", "application/json; charset=utf-8"}]
case HTTPoison.post(url, body, headers, []) do
{:ok, %HTTPoison.Response{status_code: 200, body: request_body}} ->
conn
|> put_status(200)
|> json(Jason.decode!(request_body))
{:ok, %HTTPoison.Response{status_code: 204, body: request_body}} ->
conn
|> put_status(200)
|> json(Jason.decode!(request_body))
{:error, %HTTPoison.Error{reason: reason}} ->
conn
|> put_status(400)
|> json(%{error_code: 400, data: [reason]})
end
end
,这是我的节点服务器的代码
const express = require('express');
const searchController = require('./src/controllers/searchController');
const app = express();
const http = require('http');
const port = 8050;
const host = '0.0.0.0';
app.use(express.json());
app.use((req, res, next) => {
console.log(req);
res.append('Access-Control-Allow-Origin', ['*']);
res.append('Access-Control-Allow-Methods', 'POST,OPTIONS');
res.append('Access-Control-Allow-Headers', 'Content-Type', 'Accept', 'Authorization');
next();
});
app.post('/search', searchController.list);
app.post('/search/volume', searchController.list_volume);
const server = http.createServer(app);
server.listen(port, host, () => console.log(`Listening on ${host}:${port}`));
(i知道它应该是一个httpsserver,但现在我绝望了,我想看看为什么不起作用)
应该总结一切。请让我知道,如果我忘了添加任何东西,谢谢您的宝贵时间。
This is my attempt to see if you can help me to solve an issue I've been trying to solve for a whole week.
Context:
My frontend app is connected to a single API, one in Elixir. Everything works as expected.
Elixir API is connected to another API built in node for just one endpoint (we were unable to code it in Elixir so we tried in JS)
When the frontend does a request to a specific endpoint (let's say /search), Elixir should call to that endpoint in node and return some data.
Everything works perfectly on local. Both images can communicate and data is shared between them. However, when I move to the staging environment, I always receive a econnrefused error when I do the request from my frontend environment to the same endpoint.
Both docker images are connected to the same network on staging. They can ping each other and curls work between them. But when I go through the API, there is no way to get the expected data.
docker-compose image: Node API
version: '3'
services:
web:
container_name: node_api
build: .
ports:
- '8050:80'
expose:
- '8050'
networks:
- test
command: node index.js
networks:
test:
external: true
docker-compose image: Elixir API
version: "3"
services:
web:
build: .
depends_on:
- mariadb
ports:
- "8051:8011"
environment:
MIX_ENV: "dev"
DATABASE_URL: "ecto://root:${MYSQL_ROOT_PASSWORD}@mariadb/test"
MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD}"
LC_ALL: "en_US.UTF-8"
LANG: "en_US.UTF-8"
LANGUAGE: "en_US.UTF-8"
command: "mix ecto.create && mix phx.server"
user: "root"
links:
- mariadb
networks:
- test
networks:
test:
external: true
I tried changing how the Elixir API calls the Node API, both by name (in this case node_api), by IP and nothing seems to work.
Here is a picture of how both services are running on the server (first one is node, second is elixir)
Here is the code for the Elixir API endpoint
url = "#{url}/search"
body =
Jason.encode!(%{
from_date: from_date,
search: search,
to_date: to_date,
page_size: page_size,
})
headers = [{"Content-type", "application/json; charset=utf-8"}]
case HTTPoison.post(url, body, headers, []) do
{:ok, %HTTPoison.Response{status_code: 200, body: request_body}} ->
conn
|> put_status(200)
|> json(Jason.decode!(request_body))
{:ok, %HTTPoison.Response{status_code: 204, body: request_body}} ->
conn
|> put_status(200)
|> json(Jason.decode!(request_body))
{:error, %HTTPoison.Error{reason: reason}} ->
conn
|> put_status(400)
|> json(%{error_code: 400, data: [reason]})
end
end
Here is the code for my Node server
const express = require('express');
const searchController = require('./src/controllers/searchController');
const app = express();
const http = require('http');
const port = 8050;
const host = '0.0.0.0';
app.use(express.json());
app.use((req, res, next) => {
console.log(req);
res.append('Access-Control-Allow-Origin', ['*']);
res.append('Access-Control-Allow-Methods', 'POST,OPTIONS');
res.append('Access-Control-Allow-Headers', 'Content-Type', 'Accept', 'Authorization');
next();
});
app.post('/search', searchController.list);
app.post('/search/volume', searchController.list_volume);
const server = http.createServer(app);
server.listen(port, host, () => console.log(`Listening on ${host}:${port}`));
(I know it should be a httpsServer but right now Im desperate and I want to see why is not working)
That should summarize everything. Please, let me know if I forgot to add anything and thank you for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论