【问题标题】:App crash error on trying to deploy small application to Heroku尝试将小型应用程序部署到 Heroku 时出现应用程序崩溃错误
【发布时间】:2021-08-07 15:13:18
【问题描述】:

我在部署到 heroku 时遇到此错误:

at=error code=H10 desc="App crashed" method=GET path="/" host=deploy-testv1.herokuapp.com 
request_id=b36172ff-cb1b-4436-978f-1be34e28a9e3 fwd="123.201.36.104" dyno= connect= service= 
status=503 bytes= protocol=https
2021-05-18T11:08:48.938254+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET 
path="/favicon.ico" host=deploy-testv1.herokuapp.com request_id=8e63e9a0-c621-47e0-a91d-bad7716ddde5 
fwd="123.201.36.104" dyno= connect= service= status=503 bytes= protocol=https

我的Procfile

web: gunicorn app:app

这是app.py 文件:

from flask import Flask, send_from_directory
from flask_restful import Api, Resource, reqparse
from flask_cors import CORS 
from HelloApiHandler import HelloApiHandler

app = Flask(__name__, static_url_path='', static_folder='../build')
CORS(app) #comment this on deployment
api = Api(app)

@app.route("/", defaults={'path':''})
def serve(path):
   return send_from_directory(app.static_folder,'index.html')

api.add_resource(HelloApiHandler, '/flask/hello')

HelloApiHandler.py文件:

from flask_restful import Api, Resource, reqparse

class HelloApiHandler(Resource):
def get(self):
 return {
  'resultStatus': 'SUCCESS',
  'message': "Hello Api Handler"
  }

def post(self):
print(self)
parser = reqparse.RequestParser()
parser.add_argument('type', type=str)
parser.add_argument('message', type=str)

args = parser.parse_args()

print(args)
# note, the post req from frontend needs to match the strings here (e.g. 'type and 'message')

request_type = args['type']
request_json = args['message']
# ret_status, ret_msg = ReturnData(request_type, request_json)
# currently just returning the req straight
ret_status = request_type
ret_msg = request_json

if ret_msg:
  message = "Your Message Requested: {}".format(ret_msg)
else:
  message = "No Msg"

final_ret = {"status": "Success", "message": message}

return final_ret

我的App.js 文件:

import logo from './logo.svg';
import './App.css';
import React, { useEffect, useState } from 'react';
import axios from 'axios'

function App() {
  const [getMessage, setGetMessage] = useState({})

useEffect(()=>{
 axios.get('https://deploy-testv1.herokuapp.com/flask/hello').then(response => {
  console.log("SUCCESS", response)
  setGetMessage(response)
}).catch(error => {
  console.log(error)
})

 }, [])
 return (
  <div className="App">
  <header className="App-header">
    <img src={logo} className="App-logo" alt="logo" />
    <p>React + Flask Tutorial</p>
    <div>{getMessage.status === 200 ? 
      <h3>{getMessage.data.message}</h3>
      :
      <h3>LOADING</h3>}</div>
  </header>
</div>
);
}

export default App;

我的root folder structure 视图:

内部api folder

我已经提供了所需的一切。请让我知道我做错了什么?我的 procfile 或 app.py/app.js 有问题吗?还是我的文件结构不正确?

在heroku中也添加了nodejs和python buildpack。

【问题讨论】:

  • 如果您有任何问题或者您希望查看其他文件的代码,请告诉我。这是一个小型测试项目,所以这些是主要文件。
  • 试图告诉你的错误信息是什么...?
  • 我在问题中添加了错误消息。它只是说应用程序因 H10 代码而崩溃。不知道为什么应用程序崩溃了。
  • 顺便说一句,我刚刚从我的 api 文件夹中复制了 requirements.txt 文件并粘贴到我的根文件夹中。我希望这不是问题。我刚刚在 1 个链接中看到他们将要求保存在根文件夹中。
  • 它说的不止这些,不是吗?我可以看到它包含一条路径...(您知道 favicon.ico 是什么以及为什么需要它吗?)

标签: python flask heroku


【解决方案1】:

app.py,这个小改动。而不是仅仅来自 HelloApiHandler,我将其更改为 api.HelloApiHandler。显然,在 Heroku 上部署时,我们必须保留 Procfile 作为 Python 文件的参考。 app.py文件和HelloApiHandler.py文件在同一个目录下没关系。

from api.HelloApiHandler import HelloApiHandler

在 Procfile 中。同上原因

web: gunicorn api.app:app

这部署了我的应用程序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-25
    • 2015-09-22
    • 1970-01-01
    • 1970-01-01
    • 2022-09-25
    • 2019-04-03
    • 2019-11-17
    相关资源
    最近更新 更多