我无法理解使用Sql和node.js. 文件app.js:
var connection = mysql.createConnection ({
host: 'localhost',
user: 'root',
password: '',
database: "messenger"
});
var app = express ();
app.get ('/ api', function (req, res, next) {
connection.connect ();
connection.query ('SELECT * FROM user_contacts AS uc INNER JOIN users AS u ON u.id = uc.contact_user_id WHERE uc.user_id = 1', function (err, rows, fields) {
if (err) throw err;
console.log (rows);
});
connection.end ();
});
在开始页面上与/ API说话之后。在控制台中,我看到了基地的回复。但浏览器只是挂起如何从正常的byzy获得响应并立即关闭连接?谢谢。
答案 0 :(得分:-1)
您没有发送任何回复,这就是浏览器正在等待的原因。
添加
res.end();
后
connection.end();
完整代码:
app.get ('/ api', function (req, res, next) {
connection.connect ();
connection.query ('SELECT * FROM user_contacts AS uc INNER JOIN users AS u ON u.id = uc.contact_user_id WHERE uc.user_id = 1', function (err, rows, fields) {
if (err) throw err;
console.log (rows);
});
connection.end ();
res.end();
});