首页 > 解决方案 > 我的 NodeJS 服务器在 6 个 Axios 发布请求后崩溃

问题描述

我在 ReactJS 中创建了一个前端,在 NodeJS 中创建了一个后端的应用程序。我正在做的是让前端每次在输入中输入一个字母时与后端进行通信。它工作正常(我可以在我的 NodeJS 控制台中看到我输入的每个字母),但是在此输入中敲击键盘 6 次后,我的 NodeJS 服务器崩溃,并且似乎没有响应(我没有错误)。

我试图搜索它是否不是 Axios 的问题,因为我认为一旦完成后查询它就不会断开连接,但我没有找到任何关于此的信息。

我准确地说我是 React 和 Node 的新手,所以我可能会跳过一些内容。

在 App.js 中

//This is the React Part

handleAttachmentToTask = async (event) =>{

        let self = this;

        const target = event.target;
        const keystone = target.type === 'checkbox' ? target.checked : target.value;
        const name = target.name;
        const postman = {kSent: keystone};

        axios.post('/api/sandbox',{postman})
          .then(response => {
            //self.setState({keystone:response.data});
            console.log(response.data);
          })
          .catch(error => {
            console.log(error);
        });

    }

在 server.js 中

//This is the NodeJS part

app.post('/api/sandbox', function(req, res){

    var hit = req.body.postman.kSent;
    console.log(hit);

});

我只是想在我的 NodeJS 控制台上看到键盘敲击,但目前,正如我提到的,它在 6 个字母后崩溃。

感谢帮助

标签: node.jsreactjsaxios

解决方案


您的服务器没有将响应发送回客户端。尝试发送响应。尝试更改您的服务器代码。

app.post('/api/sandbox', function(req, res){
    var hit = req.body.postman.kSent;
    console.log(hit);
    res.send(200); // Try to send response back to user 
});

推荐阅读