javascript - 加载js文件时出现404错误

标签 javascript python angularjs google-app-engine webapp2

我正在使用 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>

我收到这些错误:

  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 模式将先于 /.* 模式匹配相应路径。

关于javascript - 加载js文件时出现404错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29062563/

相关文章:

javascript - angular-ui-router 更改 $state.go() 上的数据对象

javascript - 从 foreach 循环链接 promise

javascript - 为什么 230/100*100 不返回 230?

javascript - 如何通过 websockets 在 ruby​​ on rails 和 reactJS 之间发送数据

python - bool 索引的优雅方法,列表中具有可变/动态数量的值

python - 如何使用 haversine 距离矩阵返回满足距离阈值标准的所有点对

Javascript - ('Click' ) - 错误 - 页面加载时调用

python - django 中的 Manytomany 字段的字母顺序错误

angularjs - 防止 ng-click =""对禁用按钮的影响

javascript - AngularJS:显示通过 http GET(来自 REST 服务)检索的 JSON 数据的选择框