而不是适当的响应"/>
POST请求返回index.html正文,而不是适当的响应
我一直在开发一个Web应用程序,当前端通过代理托管在本地(托管在heroku上)时,所有帖子请求均可以正常工作。但是,当在Firebase上部署前端时,请求仍会成功返回,但不是{success:true}而是从构建目录返回index.html文件,并且不会对数据库执行任何操作。
heroku上的相关代码:
const app = express();
const port = process.env.PORT || 8080;
app.use(bodyParser.json());
app.use(cors());
app.use(express.static(path.join(__dirname, 'build')));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", '*');
res.header("Access-Control-Allow-Credentials", true);
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header("Access-Control-Allow-Headers", 'Origin,X-Requested-With,Content-Type,Accept,content-type,application/json');
next();
});
app.post('/updateBoard', async (req, res) => {
const { board_id, itemName, color, index, gameInfo } = req.body;
await functions.updateBoard(board_id, itemName, color, index, gameInfo)
res.send({ success: true })
})
app.get('*', function (req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.listen(port, () => console.log(`App listening on port ${port}!`));
各自的前端功能:
const updateBoardData = async (
board_id,
itemName,
color,
index,
gameInfo
) => {
var res = await fetch("/updateBoard", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
board_id: board_id,
itemName: itemName,
color: color,
index: index,
gameInfo: gameInfo,
}),
});
console.log(res);
};
已部署的输出:<!doctype html><html lang="en"><head> ... </html>
来自本地主机的输出:{“成功”:true}
回答如下:好吧,我设法通过将获取中的路由设置为nameofproject.herokuapp/updateBoard来解决了这个问题,因为一旦部署了该应用程序后,该代理显然不起作用。
更多推荐
POST请求返回index.html正文,而不是适当的响应
发布评论