【问题标题】:app crash in appfog using node js mysql使用节点 js mysql 在 appfog 中的应用程序崩溃
【发布时间】:2013-10-07 19:38:04
【问题描述】:

我有一个使用 node-express 和 mysql 托管在 appfog 中的应用程序(以及客户端中的骨干 js),它在 localhost 中运行良好,但是当部署该应用程序时运行良好,一段时间后崩溃(如果我重新启动在 appfog admin 的控制台中也会发生同样的事情) 这是连接mysql的部分代码

var mysql = require("mysql")

var env = JSON.parse(process.env.VCAP_SERVICES);
var creds = env['mysql-5.1'][0]['credentials'];
console.log(creds)

var client = mysql.createConnection({
  host: creds.host || "localhost",
  user: creds.user,
  password: creds.password,
  port: creds.port,
  database: "savelinks"
});

app.js 中的代码

var express  = require("express");
var path = require("path");
var port = 9000;
var request = require('request');
var cheerio = require('cheerio');

var app = module.exports = express();

console.log(process.env.VCAP_SERVICES)

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);  
  app.use(express.static(path.join(__dirname, 'public')));
 });

 var server  = require("http").createServer(app)
  app.get("/", function(req,res){

    res.render("index");
 });

 /*
  * GET ALL LINKS ON LOAD PAGE
 */
 app.get("/links", function(req, res){

     links = db.client.query("SELECT * FROM links ORDER BY created DESC", function(err, result){
      if(!err){
         res.json(result)
      }
   });
});
// more routers
 app.listen(process.env.VCAP_APP_PORT || port, function(){
    console.log("server run in port " + port)
 });

这是应用雾显示的日志

events.js:71
        throw arguments[1]; // Unhandled 'error' event
                       ^
Error: Connection lost: The server closed the connection.
    at Protocol.end (/mnt/var/vcap.local/dea/apps/savelinks-0-ca19c96d36d4701debe7fe46752707c5/app/node_modules/mysql/lib/protocol/Protocol.js:73:13)
    at Socket.onend (stream.js:66:10)
    at Socket.EventEmitter.emit (events.js:126:20)
    at TCP.onread (net.js:417:51)

有解决这个问题的办法吗?

【问题讨论】:

    标签: mysql node.js backbone.js cloud-hosting appfog


    【解决方案1】:

    我在生产中遇到了同样的问题,但在我的个人开发机器上没有。我的节点服务器在 Ubuntu VM 上使用 node-mysql 和 connect。我的节点服务器会在一天中不时崩溃并重新启动,并出现与您看到的相同类型的错误。

    node-mysql GitHub页面上解释的以下方法为我解决了这个问题:

    https://github.com/felixge/node-mysql#server-disconnects

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-15
      • 1970-01-01
      • 2013-09-23
      • 2016-12-06
      • 2022-11-06
      • 1970-01-01
      • 2021-10-26
      • 2013-06-08
      相关资源
      最近更新 更多