繁体   English   中英

NodeJS + MySql请求

[英]NodeJS + MySql request

我无法理解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处获取响应并立即关闭连接的方法? 谢谢。

您没有发送任何响应,这就是浏览器正在等待的原因。

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();

});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM