我无法理解 With 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的响应并立即关闭concatenate?谢谢。
最佳答案
您没有发送任何响应,这就是浏览器等待的原因。
添加
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();
});
关于javascript - NodeJS + MySql 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32821701/