[英]NodeJS + MySql request
I can not understand With Sql and node.js. 我无法理解Sql和node.js。 a file app.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 ();
});
After I speak to / API as it happens at the start page. 在我与/ API交谈之后,它发生在起始页面上。 In the console, I see a response from the base.
在控制台中,我看到了来自主机的响应。 But the browser just hangs out how to get a response from the normal byzy and immediately close the concatenate?
但是浏览器只是挂出如何从正常的byzy处获取响应并立即关闭连接的方法? Thank you.
谢谢。
You are not sending any response, that is why the browser is waiting. 您没有发送任何响应,这就是浏览器正在等待的原因。
Add 加
res.end();
after 后
connection.end();
Full code: 完整代码:
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();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.