尝试插入值到mysql数据库,到目前为止我可以插入值,但插入后,浏览器有无限循环“等待localhost” . 我试图关闭连接 - con.end();但这并不能解决问题 .
app.post('/insert', function(req, res) {
currentdate = req.body.date;
weight = req.body.weight;
console.log("Connected!");
var sql = "INSERT INTO WEIGHT_LOG (DATE, WEIGHT) VALUES ('" + currentdate + "', '" + weight + "')";
con.query(sql, function(err, result) {
if (err) throw err;
console.log(sql);
console.log("1 record inserted");
});
});
1 回答
你的方法永远不会没有回应 .
您必须调用
res.status(200).send()
来响应HTTP 200状态代码并关闭与客户端的连接 . 您应该检查Express documentation以了解中间件的工作原理 .此外,您将"con"数据库对象与HTTP服务器和客户端之间的连接混淆 . 通过调用
con.end()
,您将关闭HTTP Server和数据库之间的连接 .