我创建了一个应用程序,前端采用 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/