【问题标题】:node.js response only one html requestnode.js 只响应一个 html 请求
【发布时间】:2011-11-13 21:05:04
【问题描述】:

我使用 node.js 和 mysql 模块来编写一个简单的选择语句。

问题是它只能响应一个请求,后续响应将是空的。

我第一次使用浏览器加载页面,它返回一个完整的结果,但浏览器仍在加载。发生了什么:

代码:

var server = http.createServer(function (request, response) {
      response.writeHead(200, {"Content-Type": "text/plain"});

      client.query('SELECT * FROM ' + tbl,
          function selectDb(err, results, fields) {
            if (err) {
              throw err;
            }

            for (var i in results){
              var result = results[i];
              response.write(result['CUSTOMERNAME']); // Writes to the web browser the value of test then a : to seperate values
            }

            response.end("END RESULT");

            client.end();
          }
      );

});

【问题讨论】:

    标签: mysql node.js


    【解决方案1】:

    根据找到here 的node-mysql 文档(我假设您正在使用),

    client.end();
    

    关闭mysql连接。

    当你尝试另一个请求时,没有打开的连接,node-mysql 不做任何连接池处理或自动重新连接,这一切都由你决定。

    如果您不介意在应用程序的整个生命周期内保持单个连接打开(不是最佳设计),您可以将 client.end() 移到连接处理程序之外。

    否则,请创建一个小方法来检查打开的连接或创建连接池,请参阅this post 了解更多信息。

    【讨论】:

      猜你喜欢
      • 2014-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-15
      • 2018-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多