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

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

我在前端应用程序下使用Python使用谷歌云平台。

我尝试从单独的文件运行 java 脚本函数,同时使用 Google App Engine Launcher 在本地计算机上进行调试。

我也使用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');
}

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

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

最佳答案

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

关于javascript - gcp上的html中的js(本地测试)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54326754/

相关文章:

javascript - 获得点击按钮的正确位置(涟漪效应)

javascript - Sencha touch 重置存储启动参数

javascript - React - 在构造函数中绑定(bind) this 不起作用

mysql - Google Cloud SQL 实例始终处于维护状态和二进制日志问题

google-cloud-platform - 如何配置多个 gcloud 项目

javascript - 如何在html中向div添加Ajax响应

python - 多处理写入 Pandas 数据框

python - Tensorflow Keras - AttributeError : Layer features has no inbound nodes

python - 如果遇到 NaN 值,将值移到 Python DataFrame 的顶部

authentication - 在Go应用程序中使用服务帐户json key 文件时获取 `Request had invalid authentication credentials`