node.js - 我的 NodeJS 服务器在 6 个 Axios post 请求后崩溃

标签 node.js reactjs axios

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

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

我准确地说,我是 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 个字母后崩溃了。

感谢帮助

最佳答案

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

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 
});

关于node.js - 我的 NodeJS 服务器在 6 个 Axios post 请求后崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56019035/

相关文章:

javascript - 将 SWR 与 Redux 一起使用是正确的还是好的做法?

javascript - 如何轻松更改上下文?

javascript - 预检响应在 axios 中没有 HTTP ok 状态

javascript - 对地点服务的所有请求都必须使用 https ://protocol, 并包含 API key

javascript - 在测试环境中触发this.props.dispatch

javascript - 从 axios 请求捕获错误时重定向

node.js - 如何在 Node.js 中获取输入的 POST 数据?

node.js - 如何使用 Node.js 的 Visual Studio Team Services 客户端 (vso-node-api) 创建工作项?

node.js - 通过 nginx 1.10.3 和 LetsEncrypt 保护 WebSocket 连接

javascript - 使用 node-amqp 模块在 Node.js 中手动确认