【问题标题】:Flask app is being run locally instead of on herokuFlask 应用程序正在本地运行,而不是在 heroku 上运行
【发布时间】:2021-08-31 16:45:20
【问题描述】:

所以我已经将我的烧瓶应用程序部署到 heroku 的反应前端,但是烧瓶在我的本地主机而不是 heroku 服务器上运行似乎存在一些问题。

我已经阅读了大量关于此的 stackoverflow 帖子,但没有解决方案。这是我的烧瓶代码:


from flask import Flask, request
import flask
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
from flask_cors import CORS

app = Flask(__name__,static_folder="./build",static_url_path="/")
app.config['SQLALCHEMY_DATABASE_URI'] = 'my database url'
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.secret_key = 'secret string'
CORS(app)
db = SQLAlchemy(app)

class Feature_votes(db.Model):
    feature = db.Column(db.String(500), primary_key=True)
    votes = db.Column(db.Integer, nullable=False)
    date = db.Column(db.DateTime, nullable=False)

    def __init__(self, feature, votes, date):
        self.feature = feature
        self.votes = votes
        self.date = date

# Serve the react app
@app.route("/")
def index():
    return app.send_static_file("index.html")

# Retrieve currently polled features from Feature_votes
@app.route("/getVotes", methods=['GET'])
def getVotes():
    rows = Feature_votes.query.filter().order_by(Feature_votes.date)
    response = []
    for row in rows:
        response.append(
            {"feature": row.feature,
             "votes": row.votes
        })
    
    return flask.jsonify(response)

# Add a new feature to the db with votes set to 0
@app.route("/featureAdd", methods=['POST'])
def featureAdd():
    feature = request.get_json()["feature"]
    featureEntry = Feature_votes(feature, 0, datetime.utcnow())
    db.session.add(featureEntry)
    db.session.commit()

    response = {"feature": featureEntry.feature,
                "votes": 0,
                "date": featureEntry.date
                }

    return response
  

@app.route("/featureModifyVotes", methods=['POST'])
def featureUnvote():
    feature = request.get_json()["feature"]
    direction = request.get_json()["direction"]
    featureEntry = Feature_votes.query.filter_by(feature=feature).first()
    
    if (direction == "increase"):
        featureEntry.votes += 1
    else:
        featureEntry.votes -= 1
    
    db.session.commit()
    response = {featureEntry.feature: featureEntry.votes}
    return response

if __name__ == '__main__':
    app.run()

这是我的 Procfile

web: gunicorn --bind 0.0.0.0:$PORT server:app

这里还有一个我从检查元素中截取的片段,以表明这个请求是在本地服务的。

我对 Web 开发比较陌生,所以我可能犯了很多错误。如果您可以提供帮助或需要我提供更多信息,请告诉我。谢谢。

【问题讨论】:

    标签: reactjs flask heroku


    【解决方案1】:

    显然,我在问题中发布的屏幕截图并不意味着我的服务器在本地主机上运行,​​而是我的请求是向本地主机发出的。原来我的构建文件中有 fetch("http://localhost...)。在使用相对路径、重建并推送到 heroku 之后,一切正常。

    【讨论】:

      猜你喜欢
      • 2019-05-24
      • 2015-01-05
      • 1970-01-01
      • 1970-01-01
      • 2021-09-18
      • 1970-01-01
      • 1970-01-01
      • 2020-02-28
      • 1970-01-01
      相关资源
      最近更新 更多