node.js - 在不同端口上运行的从前端到后端的 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.js - 如何在没有 CORS 预检的情况下向 Stripe 端点发出 POST 请求
- android - 我如何在这个 Unity 示例游戏(Trash Dash)中放置视频?
- c - 将特定结构传递给 C 中的函数
- javascript - 单击提交按钮后如何保持下拉列表的选定值
- apache-spark - 在 PySpark 中将 Parquet 文件与不同列合并
- javascript - Jest spyOn 方法
- java - 将数据添加到数据源 Wildfly
- c# - 如何处理 Azure 存储队列中的大消息?
- mongodb - 带有 $and 运算符和没有运算符的条件之间的 Mongodb 区别
- html - 使链接/按钮 100% 可点击