【问题标题】:Heroku node.js and Flask application working locally but not in serverHeroku node.js 和 Flask 应用程序在本地工作,但不在服务器中
【发布时间】:2016-10-18 12:47:14
【问题描述】:

我有一个使用 node.js 并与 Flask 应用程序通信以检索一些配置并将作业发送到后端的 Web 应用程序。 Flask 返回带有信息的 JSON

烧瓶文件(REST.PY):

from flask import Flask, jsonify, request, redirect, url_for
import subprocess

import os
import json
from cross_domain import *
app = Flask(__name__)

...

@app.route('/api/v1.0/proteins', methods=['GET', 'OPTIONS'])
@crossdomain(origin='*')
def get_protein_names():
proteins = os.walk(TARGET).next()[1]
protein_lst = [{"filename": protein} for protein in sorted(proteins) if  protein != "scripts"]
return jsonify({"files": protein_lst})

if __name__ == '__main__':
app.run(debug=True,port=9000)

这里是js代码:

...

this.restAddr = "http://127.0.0.1:9000";

...

this.httpGet = function(url, callback) {
var xmlHttp;
xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
  if (xmlHttp.readyState === 4 && xmlHttp.status === 200) {
    callback(xmlHttp.responseText);
  }
};

...

httpGet(restAddr + "/api/v1.0/proteins", populateProteins);

我目前正在使用this buildback 以便在 Procfile 中运行这两个命令。当我使用foreman start 在本地运行时,它可以工作,但是当我部署到 Heroku 时,框架不再相互通信,并且 XMLHTTPRequests 中没有响应。不过,我仍然可以使用 node.js 部分。

这是 Heroku 日志:

...
2016-06-16T20:08:59.174025+00:00 app[web.1]: buildpack=runit ps=python at=start
2016-06-16T20:08:59.174156+00:00 app[web.1]: buildpack=runit ps=node at=start
2016-06-16T20:09:04.490687+00:00 app[web.1]:  * Running on http://127.0.0.1:9000/ (Press CTRL+C to quit)
2016-06-16T20:09:04.515389+00:00 app[web.1]:  * Restarting with stat
2016-06-16T20:09:05.138990+00:00 heroku[web.1]: State changed from starting to up
2016-06-16T20:09:05.568687+00:00 app[web.1]:  * Debugger is active!
2016-06-16T20:09:05.593316+00:00 app[web.1]:  * Debugger pin code: 237-920-039
2016-06-16T20:09:06.730693+00:00 app[web.1]: Succeeded connected to: <MONGODB_URI> in port 35556

这是运行 Foreman 时出现的内容:

 $ foreman start -f Procfile.local 
14:59:30 node.1   | started with pid 21241
14:59:30 python.1 | started with pid 21242
14:59:31 python.1 |  * Running on http://127.0.0.1:9000/ (Press CTRL+C to quit)
14:59:31 python.1 |  * Restarting with stat
14:59:31 python.1 |  * Debugger is active!
14:59:31 python.1 |  * Debugger pin code: 215-168-436
14:59:31 node.1   | Succeeded connected to: mongodb://localhost/algdock in port 3000

我需要了解为什么在部署时无法正常工作,或者是否有其他更好的方法来解决此问题。

编辑:在 python 中使用 gunicorn 它给出了Error R10 (Boot timeout) -&gt; Web process failed to bind to $PORT within 60 seconds of launch

Procfile.web

web: node gui/web_app/AlGDock/bin/www
web: gunicorn --pythonpath gui/api --bind 127.0.0.1:9000 wsgi:app

wsgi.py

from REST import app
if __name__ == "__main__":
    app.run()

Heroku 日志:

2016-06-17T00:35:25.808594+00:00 heroku[web.1]: Starting process with command `bin/runsvdir-dyno`
2016-06-17T00:35:26.398041+00:00 heroku[web.1]: Process exited with status 0
2016-06-17T00:35:28.003708+00:00 app[web.1]: buildpack=runit ps=web at=start
2016-06-17T00:35:28.376799+00:00 app[web.1]: [2016-06-17 00:35:28 +0000] [15] [INFO] Starting gunicorn 19.6.0
2016-06-17T00:35:28.377454+00:00 app[web.1]: [2016-06-17 00:35:28 +0000] [15] [INFO] Listening at: http://127.0.0.1:9000 (15)
2016-06-17T00:35:28.377583+00:00 app[web.1]: [2016-06-17 00:35:28 +0000] [15] [INFO] Using worker: sync
2016-06-17T00:35:28.385995+00:00 app[web.1]: [2016-06-17 00:35:28 +0000] [20] [INFO] Booting worker with pid: 20
2016-06-17T00:35:28.826322+00:00 app[web.1]: /app/target/
2016-06-17T00:35:28.826340+00:00 app[web.1]: /app/AlGDock/
2016-06-17T00:36:25.826633+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch

【问题讨论】:

  • 完整性检查,您正在restAddr更改为新的服务器位置,不是吗?
  • @slugonamission http://127.0.0.1:9000 地址在同一个地方不应该有效吗?
  • 您是否将这两个文件托管在同一个(虚拟)服务器上?否则,您需要获取其他服务器地址并将其分配给 restAddr,例如 restAddr=module.gettingPort()||127.0.0.1。您可能会从一个新的 Heroku 应用模板中获得灵感。
  • 是的@sp3 我将它们托管在同一台服务器上,并使用localhost 作为restAddr。我尝试了this 解决方案,但使用 Flask 而不是 Django。也许我应该尝试改变它,看看它是否有效。
  • 你是在开发环境还是生产环境运行Node?在heroku上的开发环境中运行节点。

标签: javascript python node.js heroku flask


【解决方案1】:

如果您正在运行网络测功机,那么 heroku 需要能够在 60 秒内连接到它,否则它会假定它不工作并关闭测功机。 heroku 使用的端口未设置,因此您需要从 dynos 环境中获取端口我在开始时遇到了同样的错误,因为您无法控制 heroku 分配的端口。

node+express版本是 app.set('port', (process.env.PORT));

这个问题展示了使用flask时如何获取这个值Deploying Flask app to Heroku

编辑: 也来自 heroku python 文档

port = int(os.environ.get("PORT", 5000)) app.run(host='0.0.0.0', port=port)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-29
    • 2015-07-24
    • 1970-01-01
    • 2016-05-23
    • 2014-11-16
    • 1970-01-01
    • 2019-06-19
    • 2021-10-01
    相关资源
    最近更新 更多