2013-10-01 37 views
0

我有一个应用程序使用node-express和mysql(以及客户端的骨干js)托管在appfog中,它在localhost中正常工作,但部署应用程序时工作良好,一时间崩溃(如果我在appfog管理控制台重新启动同样的事情发生)后 这是连接mysql的部分代码使用节点js的mysql appfog中的应用程序崩溃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) 

解决此问题的任何想法?

回答

0

我在生产中遇到了同样的问题,但不是在我的个人开发机器上。我的节点服务器使用node-mysql与Ubuntu VM上的连接。我的节点服务器会崩溃并立即重新启动,然后在整个一天中发生相同类型的错误。

以下方法节点-mysql的GitHub的页面上解释工作,以解决这个问题对我来说:

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