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

来源:3-10 深入了解flask路由

abulaka

2019-05-08

在前后端分离的开发中,前端代码使用的是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.

写回答

1回答

7七月

2019-05-09

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

0
2
7七月
回复
abulaka
我记得flask对于这个icon是有方法处理的,可以看看flask的文档。
2019-05-09
共2条回复

Python Flask高级编程之从0到1开发《鱼书》精品项目

7月老师深入浅出剖析Flask核心机制,和你一起探讨Python高级编程

2751 学习 · 1277 问题

查看课程