我有一个使用node-express和mysql(以及客户端中的 Backbone js)托管在appfog中的应用程序,它在本地主机中运行良好,但是当部署时,该应用程序运行良好一段时间并在崩溃后(如果我重新启动在 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)
有什么办法可以解决这个问题吗?
最佳答案
我在生产中遇到了同样的问题,但在我的个人开发机器上没有。我的 Node 服务器在 Ubuntu VM 上使用 node-mysql 进行连接。我的 Node 服务器会时不时地崩溃并重新启动,并出现与您所看到的相同类型的错误。
node-mysql GitHub 页面上解释的以下方法可以帮助我解决问题:
关于mysql - 使用 Node js mysql 在 appfog 中应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19119662/