[英]Heroku Node.js application fails to start
我一直在开发我的第一个Node.js应用程序(实际上是将一些旧代码移到Node上进行娱乐和练习),并且正在将其托管在Heroku上。
我已经能够使用领班在本地运行该应用程序,但是每当我将该应用程序部署到Heroku时,它就会启动,但无法提供查询服务。 我已经做过一些研究,找不到任何特定的东西, 但是在我看来,Heroku尚未正确安装几个模块 ?
MySQL数据库托管在Amazon RDS中,并且我已经按照并重新检查了步骤,访问权限和安全例外,似乎没有问题。
我正在使用package.json中定义的以下模块:
{
"name": "AppName",
"version": "0.0.1",
"dependencies": {
"express": "2.5.x",
"jade": "0.28.1",
"stylus": "0.32.0",
"mysql": "2.0.0-alpha7",
"string": "1.2.*",
"sanitizer": "0.0.*",
"validator": "0.4.8"
},
"engines": {
"node": "0.8.*",
"npm": "1.*.*"
}
}
在我的应用文件顶部调用:
var express = require('express');
var crypto = require('crypto');
var S = require('string');
var mysql = require('mysql');
var sanitizer = require('sanitizer');
var check = require('validator').check,
sanitize = require('validator').sanitize;
错误消息如下:
TypeError: Cannot call method 'query' of undefined
at pool.getConnection.app.get.connection.query.res.render.global_title (/app/web.js:40:14)
at callbacks (/app/node_modules/express/lib/router/index.js:272:11)
at param (/app/node_modules/express/lib/router/index.js:246:11)
at pass (/app/node_modules/express/lib/router/index.js:253:5)
at Router._dispatch (/app/node_modules/express/lib/router/index.js:280:5)
at Object.middleware [as handle] (/app/node_modules/express/lib/router/index.js:45:10)
at next (/app/node_modules/express/node_modules/connect/lib/http.js:204:15)
at Object.stylus (/app/node_modules/stylus/lib/middleware.js:181:7)
at next (/app/node_modules/express/node_modules/connect/lib/http.js:204:15)
at Object.methodOverride [as handle] (/app/node_modules/express/node_modules/connect/lib/middleware/methodOverride.js:35:5)
和失败的代码段:
pool.getConnection(function(err, connection) {
app.get('/', function(req, res){
connection.query('QUERY_HERE', function(err, rows, fields) {
if (err) throw console.log(err);
res.render('index', {
global_title: S(site_title).stripTags().s,
title: S(rows[0].title).stripTags().s,
bodytext: sanitize(sanitizer.sanitize(rows[0].content)).xss()
});
connection.end();
});
});
//end connection
});
这是Heroku在浏览器或日志中给我的唯一错误,并且在构建它时似乎没有任何问题。
我在这里有些茫然,所以希望有人能指出我到底是个白痴的位置,并在我诉诸Heroku之前让我滚动一下!
提前致谢。
更新:
使用Pascal Belloncle建议的代码更改来捕获日志中的错误会在浏览器中产生应用程序错误,并在日志中产生以下错误:
connection.query(QUERY, function(err, row
at Handshake.Sequence.end (/app/node_modules/mysql/lib/protocol/sequences/Sequence.js:66:24)
at Protocol.handleNetworkError (/app/node_modules/mysql/lib/protocol/Protocol.js:230:14)
at Socket.EventEmitter.emit (events.js:126:20)
at Socket._destroy.self.errorEmitted (net.js:328:14)
TypeError: Cannot call method 'query' of undefined
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
at Handshake.Pool.getConnection [as _callback] (/app/node_modules/mysql/lib/Pool.js:35:9)
at Connection._handleNetworkError (/app/node_modules/mysql/lib/Connection.js:145:18)
at /app/web.js:40:15
似乎还是在同一时间崩溃,没有有用的错误。
更新2:
错误信息!
2013-02-06T06:32:57+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=GET path=/ host=trash-media.herokuapp.com fwd=31.3.253.10 dyno=web.1 queue=0ms wait=0ms connect=4ms service=21033ms status=503 bytes=0
https://devcenter.heroku.com/articles/error-codes#h13-connection-closed-without-response
更新3:
在看到来自更新2的错误消息并对RDS中的安全设置进行了一些摆弄之后,现在看来这与托管Heroku(美国西部)和我的RDS数据库所在的可用性区域(欧盟)之间的差异有关。 -西方)。 我已经在Heroku的支持下提出了这个问题,如果有答案,我会以一个答案结束这个问题,并希望能找到一个可行的解决方案或解决方法。
在第一行检查err的值。 如果连接未定义,则最有可能是因为您遇到错误。
另外,您应该在每个请求上获得一个新的连接,然后返回到池中。
app.get('/', function(req, res){
pool.getConnection(function(err, connection) {
if (err) {
console.error("getConnection", err);
}
// do stuff here
connection.end(); // return connection to the pool
});
});
由于这个问题现在似乎与AWS在Heroku应用程序位于(美国)和RDS位于(欧盟)之间的可用性区域的差异有关,因此,我正在关闭此问题。
尽管按照RDS开发人员工具演练中的建议更改了区域,但Heroku仍然无法与我的RDS进行交互。
解决方法是更改RDS数据库的安全组策略以允许所有连接:
0.0.0.0/0
或将数据库移到美国可用性区域。
不太理想。
我现在已经等了四天了,以得到Heroku支持人员的任何其他建议的答复,因此,如果他们回来了,我会将它们添加到此答案中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.