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是什么意思?
022019-05-09
相似问题