简体   繁体   English

我在 Heroku 上收到 404 错误,但它在本地工作

[英]I get a 404 error on Heroku but it works locally

My app works locally (when running Heroku local in the terminal), with the correct routes.我的应用程序在本地运行(在终端本地运行 Heroku 时),路线正确。 However when I move onto the Heroku server the app is built fine but then just shows a 404 error when I go onto the app.但是,当我移至 Heroku 服务器时,应用程序构建良好,但当我将 go 移至应用程序时,仅显示 404 错误。 The Heroku.logs return something like this for whichever page I go onto. Heroku.logs 对于我 go 到的任何页面都会返回类似的内容。

2020-07-16T17:24:44.233685+00:00 heroku[router]: at=info method=GET path="/" host=advancedmatchedbetting.herokuapp.com request_id=315ee036-cb58-4493-9b46-c2a25337070e fwd="176.250.1.224" dyno=web.1 connect=1ms service=3ms status=404 bytes=400 protocol=https

I will as little code as I think is needed here to keep it succinct, if there's any more that would be useful to you then let me know and ill put it up also.我将尽可能少的代码来保持它的简洁,如果还有更多对你有用的东西,请告诉我,我也会把它贴出来。 The git commit/push/add are all upto date. git 提交/推送/添加都是最新的。

Procfile档案

web: cd api && export APP_SETTINGS=config.cfg && gunicorn wsgi:app --log-file -

wsgi.py wsgi.py

from application.__init__ import create_app

app = create_app()

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

init .py初始化.py

db = SQLAlchemy()

def create_app():
    app = Flask(__name__, static_folder='/Users/ianholdroyd/Documents/GitHub/advancedmatchedbetting_react/build', static_url_path='/')
    db.init_app(app)
    app.config.from_envvar('APP_SETTINGS')
    with app.app_context():
        from . import routes
        db.create_all()
        return app

routes.py路线.py

@app.route('/')
def index():
    return app.send_static_file('index.html')

@app.route('/api/signup', methods=['POST'])
def signup():
    data = request.get_json()
    new_user = newsletter(
            first_name=data['firstName'],
            last_name=data['lastName'],
            email= data['email'],
            created=dt.now()
        )
    db.session.add(new_user)  # Adds new User record to database
    db.session.commit()
    return("")

config.cfg配置文件

SECRET_KEY = ****
SQLALCHEMY_TRACK_MODIFICATIONS = False
DEBUG = True
SQLALCHEMY_DATABASE_URI = 'sqlite:///static/site.db'

app.js应用程序.js

function App() {

    return (
      <Router>
        <Switch>
          <Route exact path="/" component={homepage} />
          <Route exact path = "/404" component={NotFoundPage} />
          <Route exact path="/calculator/" component={Calculator} />
          <Route exact path="/blogpost/:id" component={blogpostpage} />
          <Route exact path="/signup" component={signuppage} />
          <Redirect to="/404"/> 
        </Switch>
        </Router>
      );
    }


export default App;

Finally managed to sort this.终于设法对此进行了排序。 In case anyone else needs help.以防其他人需要帮助。

There was a few issues with this.这有一些问题。 I first managed to allow navigation on the local version by changing my routes to this:我首先设法通过将路线更改为允许在本地版本上导航:

@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def catch_all(path):
    return app.send_static_file('index.html')

@app.errorhandler(404)   
def not_found(e):   
  return app.send_static_file('index.html')

Then the other issue was that I had declared the static folder as one from my laptop as opposed to a relative one.然后另一个问题是我已将 static 文件夹声明为我的笔记本电脑中的文件夹,而不是相对文件夹。 I changed init .py to this which solved the issue:我将init .py 更改为此解决了问题:

app = Flask(__name__, static_folder=os.path.abspath("../build"), static_url_path='/')

I think that all of this pain could have been avoided by using something like Nginx, but I couldn't quite get my head around it so this will do as a workaround for now!我认为使用 Nginx 之类的东西可以避免所有这些痛苦,但我无法完全理解它,所以现在这将作为一种解决方法!

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

相关问题 使用部署到Heroku的Django应用程序时出现内部服务器错误。 该应用程序在本地工作 - I have an internal server error when using Django app deployed to Heroku. The app works locally Discord.py 机器人在本地工作,但是当我将它推送到 heroku 时,它给出了一个错误 - Discord.py bot works locally but when I push it to heroku it gives an error 无法让我的基于 NLTK 的聊天机器人在 heroku 上运行,但它可以在本地运行 - Cannot get my NLTK based Chatbot run on heroku but it works locally git push后的heroku server 500错误,尽管在本地工作 - heroku server 500 error after a git push it works locally though 请求在本地工作,但未部署在 Heroku - Requests works locally but not deployed on Heroku Streamlit 应用程序可以在本地运行,但不能在 Heroku 上运行 - Streamlit app works locally but not on Heroku Django 应用程序在本地运行,但我在 Heroku 上收到 CSRF 验证失败 - Django app runs locally but I get CSRF verification failed on Heroku Flask 应用程序可以在本地运行,但不能在 Heroku 上运行(不允许使用该方法) - Flask app works locally, but not on Heroku (Method not allowed) Heroku 在本地工作但在部署时崩溃 - Flask - Heroku works locally but crashes when deployed - Flask Python PIL JPEG支持适用于Heroku,但不适用于本地 - Python PIL JPEG support works on Heroku but not locally
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM