首页 > 解决方案 > 在不同端口上运行的从前端到后端的 Api 调用

问题描述

我有前端项目在不同的端口上运行,然后是后端项目。

我的 nextjs 项目在端口 3990 上运行,nodejs 在端口 3991 上运行。从我的前端,我想对后端进行 api 调用,我要做的是:

例子 :

export const signup = async (user) => {
  return fetch(`http://localhost:3991/api/signup`, {
    method: 'POST',
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(user),
  })
    .then((response) => {
      return response.json();
    })
    .catch((err) => {
      return { status: 'fail', message: 'API CALL ERROR', error: err.message };
    });
};

我收到这个:

Failed to load resource: net::ERR_CONNECTION_REFUSED

在开发中正在工作,但现在在生产中却没有。

前端有自己的域,后端只是在本地主机上运行

标签: node.jsreactjsnext.jsfrontendbackend

解决方案


推荐阅读