我正在使用 webapp2 和 angularjs 构建一个 web 应用程序。这是目录结构。
|--test-app
|--lib
|--static
|--js
|--app.js
|--controller.js
|--lib
|--angular
|--angualr-bootstrap
|--index.html
|--app.yaml
|--mainapp.py
但是当我尝试加载index.html中的js文件
<!DOCTYPE html>
<html lang="en" ng-app="testApp">
<head>
<script src="/static/js/app.js"></script>
<script type="text/javascript" src="/static/js/controller.js"></script>
</head>
<body>
<div ng-controller="MainController">
IN MAIN
</div>
</body>
</html>
我收到这些错误:
GET http://localhost:8080/static/js/app.js
(404 - 找不到文件)GET http://localhost:8080/static/js/controller.js
(404 - 找不到文件)
我不明白为什么会出现这些错误。
这是app.yaml
的代码
application: test-app
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: mainapp.app
libraries:
- name: webapp2
version: "2.5.2"
这是mainapp.py
的代码
class Start(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/html'
self.response.write(open('static/index.html').read())
app = webapp2.WSGIApplication([
('/', Start),
], debug=True)
最佳答案
您必须在 app.yaml
中显式声明静态内容的位置:
handlers:
- url: /static
static_dir: static
handlers:
- url: /.*
script: mainapp.app
参见docs详情:
Unlike a traditional web hosting environment, Google App Engine does not serve files directly out of your application’s source directory unless configured to do so.
URL 处理程序路径模式按照它们在 app.yaml
中出现的顺序从上到下进行测试。在这种情况下,/static
模式将先于 /.*
模式匹配相应路径。
关于javascript - 加载js文件时出现404错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29062563/