gcp上的html中的js(本地测试)不起作用

时间:2019-01-23 11:57:33

标签: javascript python google-cloud-platform app.yaml

我在使用python的前端应用程序下使用Google Cloud Platform。

当我使用Google App Engine Launcher在本地计算机上调试时,尝试从单独的文件运行Java脚本功能。

我也使用python。

我在同一文件夹中有四个文件:

app.yaml:

application: test
version: 1
runtime: python27
api_version: 1
threadsafe: yes

handlers:
- url: .*
  script: main.app

- url: /.js
  static_files: /test.js
  upload: /test.js  

libraries:
- name: webapp2
  version: "2.5.2"
- name: jinja2
  version: latest
- name: markupsafe
  version: latest

main.html:

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>HELLO</title>

  <script type="text/javascript" src="test.js"></script>

</head>

<body>
    <input type="button" name="Test" value="Test" onclick="testclick()"/>
</body>

</html>

main.py:

import jinja2
import os
import webapp2

template_env = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.getcwd()))

class MainHandler(webapp2.RequestHandler):
    def get(self):
        template = template_env.get_template('main.html')        
        self.response.out.write(template.render())

app = webapp2.WSGIApplication([
    ('/', MainHandler)
], debug=True)

和test.js:

function testclick()
{
    alert('module test.js');
}

我收到一个错误 GET http://localhost:8080/test.js net :: ERR_ABORTED 404(未找到) 在点击按钮时。

我应该如何修改yaml文件来解决此问题?

1 个答案:

答案 0 :(得分:0)

app.yaml文件中处理程序的顺序很重要,您的/.js条目将永远不会被命中,因为它将首先与.*模式匹配,您需要将它们反转订单。

所以,我编辑了您的app.yaml文件,它对我来说很好用。

application: test
version: 1
runtime: python27
api_version: 1
threadsafe: yes

handlers:
- url: /test.js
  static_files: test.js
  upload: test.js  

- url: .*
  script: main.app

libraries:
- name: webapp2
  version: "2.5.2"
- name: jinja2
  version: latest
- name: markupsafe
  version: latest