为什么服务器向自己发送请求?

时间:2021-07-17 06:03:41

标签: node.js express file-upload deployment fetch

我在快递服务器 A 中有一项服务,我必须将图片发送到另一个快递服务器 B。

代码如下:

export const savePhotoTest = async (_req: Request, resp: Response) => {
  const filePath = path.join('path', 'to', 'file');

  const stats = fs.statSync(filePath);
  const fileSizeInBytes = stats.size;
  const fileStream = fs.readFileSync(filePath);

  fetch("http://other.server/upload", {
    method: 'POST', 
    headers: {
      "Content-Length": `${fileSizeInBytes}`,
    },
    body: fileStream
  })
  .then((res) => {
    resp.status(200);
    resp.json({ msg: "Completed!" });
  })
  .then((json) => {
    console.log(json);
  })
  .catch((err) => {
    resp.status(500);
    resp.json({ msg: "An error ocurred" });
  })
}

问题是请求返回 200,但图片永远不会到达服务器 B,并且记录我发现服务器似乎向它自己发出请求。我得到类似的信息:

POST /upload 200 XX.XX ms - 22
GET /original/service 200 XX.XX ms - -

因此,服务器似乎正在向自身发送 /upload 请求,而应该将其发送到另一台服务器,因为我已正确指定了 url。

我想知道是否有任何防火墙配置或将请求发送回服务器 A 而不是将其发送到服务器 B 的东西。

有什么想法吗?

感谢您的帮助。

0 个答案:

没有答案