加载js文件时出现404错误

404 error while loading js files

我正在使用 webapp2 构建带有 angularjs 的网络应用程序。这是目录结构。

|--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>

我收到这些错误:

  1. GET http://localhost:8080/static/js/app.js(404 - 未找到文件)

  2. 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 模式将在 /.* 模式之前匹配到适当的路径。

我将 index.html 移出静态文件夹,然后明确声明静态 url,如 @Selcuk 所说。现在可以使用了。