简体   繁体   English

Heroku Node.js应用程序无法启动

[英]Heroku Node.js application fails to start

I've been developing my first Node.js application (actually, moving some old code to Node for fun and practice) and am playing with hosting it on Heroku. 我一直在开发我的第一个Node.js应用程序(实际上是将一些旧代码移到Node上进行娱乐和练习),并且正在将其托管在Heroku上。

I've been able to get the application as it stands running swimmingly locally using foreman, but whenever I deploy the app to Heroku it starts, but fails to serve the queries. 我已经能够使用领班在本地运行该应用程序,但是每当我将该应用程序部署到Heroku时,它就会启动,但无法提供查询服务。 I've done some research and can't find anything specific, but it looks to me like a couple of modules haven't been correctly installed by Heroku ? 我已经做过一些研究,找不到任何特定的东西, 但是在我看来,Heroku尚未正确安装几个模块

The MySQL database is hosted in Amazon RDS, and I've already followed and rechecked the steps, access rights and security exceptions and there doesn't seem to be an issue. MySQL数据库托管在Amazon RDS中,并且我已经按照并重新检查了步骤,访问权限和安全例外,似乎没有问题。

I'm using the following modules, defined in my package.json: 我正在使用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.*.*"
 }
}

Called at the top of my app file: 在我的应用文件顶部调用:

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;

The error message is as follows: 错误消息如下:

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)

And the segment of code that is failing: 和失败的代码段:

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
});

This is the only error that Heroku gives me in the browser or logs, and it doesn't seem to have any trouble building it. 这是Heroku在浏览器或日志中给我的唯一错误,并且在构建它时似乎没有任何问题。

I'm kind of at a loss here, so hoping someone can point out exactly where I'm being an idiot and get me rolling before I resort to contacting Heroku! 我在这里有些茫然,所以希望有人能指出我到底是个白痴的位置,并在我诉诸Heroku之前让我滚动一下!

Thanks in advance. 提前致谢。

Update: 更新:

Using a code change suggested by Pascal Belloncle to capture the error in the logs yields an application error in the browser and the following in the log: 使用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

Still seems to be crashing at the same point with no useful error. 似乎还是在同一时间崩溃,没有有用的错误。

Update 2: 更新2:

An error message! 错误信息!

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 https://devcenter.heroku.com/articles/error-codes#h13-connection-closed-without-response

Update 3: 更新3:

After seeing the error message from update 2 and doing some fiddling with the security settings in RDS, this now looks like it is related to the difference in availability zone between where Heroku is hosted (us-west) and where my RDS DB is (eu-west). 在看到来自更新2的错误消息并对RDS中的安全设置进行了一些摆弄之后,现在看来这与托管Heroku(美国西部)和我的RDS数据库所在的可用性区域(欧盟)之间的差异有关。 -西方)。 I've raised it with Heroku support and will close this question with an answer should I get one, and hopefully a working solution or workaround. 我已经在Heroku的支持下提出了这个问题,如果有答案,我会以一个答案结束这个问题,并希望能找到一个可行的解决方案或解决方法。

Check the value of err on the first line. 在第一行检查err的值。 If connection is undefined, it is most likely because you have an error. 如果连接未定义,则最有可能是因为您遇到错误。

Plus, you should get a new connection on each request, and then return to the pool. 另外,您应该在每个请求上获得一个新的连接,然后返回到池中。

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
  });
});

I'm closing this question as the issue now appears to be related to the difference in availability zones in AWS between where the Heroku app is (US) and where the RDS is (EU). 由于这个问题现在似乎与AWS在Heroku应用程序位于(美国)和RDS位于(欧盟)之间的可用性区域的差异有关,因此,我正在关闭此问题。

Despite changing the zone as suggested on the RDS developer tools walkthrough, Heroku still cannot interact with my RDS. 尽管按照RDS开发人员工具演练中的建议更改了区域,但Heroku仍然无法与我的RDS进行交互。

The workaround for this is to change security group policies for the RDS database to allow all connections: 解决方法是更改​​RDS数据库的安全组策略以允许所有连接:

0.0.0.0/0

Or move the database to the US availability zone. 或将数据库移到美国可用性区域。

Hardly ideal. 不太理想。

I've now been waiting four days for a reply from Heroku support with any other suggestions, so if they come back I will add them to this answer. 我现在已经等了四天了,以得到Heroku支持人员的任何其他建议的答复,因此,如果他们回来了,我会将它们添加到此答案中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM