Node.js 使用reactjs将axios POST方法发送到节点路径的过程是什么?
我正在尝试使用axios向后端发送POST请求,但它抛出了404作为路径,我不知道为什么 下面是调用axios请求的react/redux代码Node.js 使用reactjs将axios POST方法发送到节点路径的过程是什么?,node.js,reactjs,firebase,axios,google-cloud-functions,Node.js,Reactjs,Firebase,Axios,Google Cloud Functions,我正在尝试使用axios向后端发送POST请求,但它抛出了404作为路径,我不知道为什么 下面是调用axios请求的react/redux代码 export const addGoal = (newGoal: Goal) => { return (dispatch: any) => { authMiddleWare(history) const newValues = newGoal const authToken = localStorage.getIt
export const addGoal = (newGoal: Goal) => {
return (dispatch: any) => {
authMiddleWare(history)
const newValues = newGoal
const authToken = localStorage.getItem('AuthToken')
axios.defaults.headers.common = { Authorization: `${authToken}` }
axios
.post('/goal', newValues)
.then((response) => {
console.log('success', response.data)
dispatch({
type: ADD_GOAL,
payload: response.data,
})
})
.catch((err) => {
console.error('\nCould not submit goal\n', err.response)
})
}
}
这是我在主后端文件中用于调用路径的nodejs路径
app.post("/goal", auth, postOneGoal);
这是节点路径的后端函数
// ADDS A SINGLE WORKOUT
exports.postOneGoal = (request, response) => {
if (request.body.id.trim() === "" || request.body.text.trim() === "") {
return response.status(400).json({ body: "Must not be empty" });
}
const newGoalItem = {
username: request.user.username,
id: request.body.id,
text: request.body.text
};
db.collection("goals")
.add(newGoalItem)
.then((doc) => {
const responseNewGoalItem = newGoalItem;
responseNewGoalItem.id = doc.id;
doc.update(responseNewGoalItem);
return response.json(responseNewGoalItem);
})
.catch((err) => {
response.status(500).json({ error: "Couldn't add the goal" });
console.error(err);
});
};
我还在package.json中使用firebase url代理
如果需要更多信息,请告知我根据评论将此作为社区Wiki发布
考虑到您正在使用云函数,每次更新代码时都需要重新部署这些函数。您可以在可访问的官方文档中查看有关部署功能的更多详细信息。在那里,您可以选择如何以及在何处部署功能以更好地进行测试。您使用的是firebase托管和/或功能吗?我使用的是firebase功能。添加新路由时是否必须重新部署它们?代码发生更改时,您必须随时重新部署。如果您在开发过程中使用本地服务器,它是否会自行刷新?如果您想在本地主机上测试,我建议使用firebase