我在appfog中使用node-express和mysql(以及客户端的骨干js)托管了一个应用程序,它在localhost中工作正常,但在部署时,应用程序工作正常并且在一段时间后崩溃(如果我重新启动)在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)
});
这是app fog显示的日志
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 :(得分:0)
我在制作中遇到了同样的问题,但在我的个人开发机器上却没有。我的节点服务器在Ubuntu VM上使用node-mysql和connect。我的节点服务器会在一天之内崩溃并重新启动,并且会出现同样的错误。
在node-mysql GitHub页面上解释了以下方法,为我解决了这个问题: