请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

404 Not Found: The requested URL was not found on the server.

在前后端分离的开发中,前端代码使用的是react,后端flask提供服务
假如把react的代码通过react提供的方式(react-scripts build)进行编译
编译后的结果如图所示,都放在build文件夹下
图片描述
在flask中,将build/static文件夹设置为static_folder,这样就可以通过 http://localhost:5000/static/css/… 方式进行进行静态文件访问
将build文件夹设置为template_folder,这样就可以通过在flask的render_template进行渲染

服务端代码

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

@app.errorhandler(Exception)
def handle_error(e):
    bizMessage = "unexpected Error"
    logger.exception(f"{bizMessage}:{e}") 
    return json.dumps(makeErrorResponse(bizMessage))

index.html代码

<!DOCTYPE html>
<html lang="en">
  <head>
    <link rel="manifest" href="/manifest.json">
    <link rel="shortcut icon" href="/favicon.ico">
  </head>
  <body>
    <div id="root"></div>
  </body>
</html>

现在的问题是
因为在index.html中这样使用的话,会把 favicon.ico 和 manifest.json 认为成一个service
而我在flask中并没有定义这个service,
所以在 errorhandler 中会抛出 404
有没有什么办法可以使用如下方式直接访问
http://localhost:5000/manifest.json
http://localhost:5000/favicon.ico
或者让 errorhandler 不抛出 404 错误

错误消息
unexpected Error:404 Not Found: The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

正在回答 回答被采纳积分+3

1回答

7七月 2019-05-09 08:25:02

我不太理解你说的,把favor.ico认为是一个service是什么意思?

0 回复 有任何疑惑可以回复我~
  • 提问者 abulaka #1
    因为 favicon.ico 并不在 flask 的 static_folder 下,所以直接访问 http://localhost:5000/favicon.ico 或者 http://localhost:5000/static/favicon.ico 的话,flask 会认为访问的资源不是静态资源,而是一个api调用
    回复 有任何疑惑可以回复我~ 2019-05-09 08:47:39
  • 7七月 回复 提问者 abulaka #2
    我记得flask对于这个icon是有方法处理的,可以看看flask的文档。
    回复 有任何疑惑可以回复我~ 2019-05-09 12:56:52
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号