简体   繁体   English

在 Herokuapp 中对节点服务器进行 React Fetch

[英]React Fetch to Node Server in Herokuapp

I'm attempting to fetch data from my node.js server (which is on port 7000) through react in heroku with this code:我正在尝试通过在 heroku 中使用以下代码做出反应,从我的 node.js 服务器(位于端口 7000)中获取数据:

const { name } = e.target.elements;
        let details = {
            name: name.value,
        };
        let response = await fetch("/post", {
            method: "POST",
            headers: {
                "Content-Type": "application/json;charset=utf-8",
            },
            body: JSON.stringify(details),
        });

and this server setup:这个服务器设置:

if(process.env.NODE_ENV === "production") {
    app.use(express.static('../client/build'));
    app.get('*', (req, res) => {
        req.sendFile(path.resolve('client/build/index.html', { root: "../" }));
    });
}

app.post("/post", async (req, res) => {
    const queryRes = await searchFromQuery(req.body.name);

    res.json({
        status: queryRes.isFound ? "found" : "notFound", teaminfo: {
            name: queryRes.name,
            number: queryRes.number,
            avgScore: queryRes.avgScore,
            predictedScore: queryRes.predictedScore,
        }
    });
});

app.listen(PORT, console.log(`Server started on port ${PORT}`));

app.get("/", function(req, res) {
    res.sendFile('client/build/index.html', { root: "../" });
});

However, when I print out req there doesn't seem to be any data related to the react request.但是,当我打印出 req 时,似乎没有任何与反应请求相关的数据。 I believe it has to do with my express setup (which I set to express.static() for heroku, though it was initially express.json())我相信这与我的快速设置有关(我为 heroku 设置为 express.static(),尽管它最初是 express.json())

Additionally, the proxy in my package.json is still set to http://localhost:7000 .此外,我的 package.json 中的代理仍设置为http://localhost:7000

Issue resolved by adding app.use(express.json()) back to the server file alongside static.通过将app.use(express.json())与 static 一起添加回服务器文件解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React Native 使用 fetch 将数据发送到 Node.js 服务器 - React Native send data to Node.js server with fetch React/Node js 应用程序在开发中工作,但在生产中不工作。 不断收到“POST https://phlatt.herokuapp.com/send 404(未找到)”错误 - React/Node js app works in development but not in production. Keep getting "POST https://phlatt.herokuapp.com/send 404 (Not Found)" Error HerokuApp Node.js Express获得TCP - HerokuApp Node.js express get TCP React App使用Node Fetch获取数据 - React App fetch data using Node Fetch 连接到parse到herokuapp.com/parse服务器? - Connect to Parse to herokuapp.com/parse server? 无法使用React.js和Node.js在服务器端获取用户输入值 - Could not fetch the user input value at server side using React.js and Node.js 反应 js c'ant 从数据库中获取 - 节点 js api 服务器不断加载 - react js c'ant fetch from database - node js api server keeps loading 在React中使用Fetch从服务器获取数据 - Using Fetch in React to fetch data from server React Native 使用 express 和 multer 将多个图像上传到节点 js 服务器 - React Native uploading multiple images with fetch to node js server with express and multer 使用节点/反应的服务器端渲染。 如何获取数据? - Server-side rendering with Node/React. How do I fetch the data?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM