gpt4 book ai didi

html - 如何在 Google App Engine 上只提供静态文件?

转载 作者:可可西里 更新时间:2023-11-01 13:13:45 25 4
gpt4 key购买 nike

我用 HTML5 写了一个游戏。在本地,它仅在我运行时有效:

python -m SimpleHTTPServer

然后我打开 localhost:8000。所以,只有一堆 .html 和 .js 文件是行不通的。我想把我的游戏放到网上,因为这个 Github (Pages) 是不可能的,因为它不会工作。

这是我需要服务器的代码部分(我确实意识到 localhost:8000/res/ 不能在 App Engine 上运行,我需要更改地址) :

var mapFile = new XMLHttpRequest();
var self = this;
mapFile.open("GET", "http://localhost:8000/res/map" + mapNumber.toString() + ".txt", true);

mapFile.onreadystatechange = function() {
if (mapFile.readyState === 4) {
if (mapFile.status === 200) {
self.lines = mapFile.responseText.split("\n");
self.loadTilesFromLines();
}
}
};

mapFile.send(null);

所以,我听说 Google App Engine 可以,它支持 Python,而且很受欢迎。现在,我不需要他们文档中的任何东西(写得很好):

import webapp2

class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('Hello, webapp2 World!')

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

我只需要一个 SimpleHTTPServer,它允许我在 my-app.appspot.com 上打开我的 index.html

我确实尝试了这个示例并启动并运行了它,但我无法强制我的浏览器打开 index.htmlsrc/ 甚至 res/.

因此,我什至不确定 Google App Engine 是否支持我在这里尝试实现的目标。该文档仅关注构建使用 Python 的应用程序,而我对 Python 只需要一个 SimpleHTTPServer,我认为 App Engine 不需要它。

最佳答案

是的,对于您要在这里实现的目标而言,这是非常可行的。由于您只想提供静态文件,因此非常简单,您不需要包含任何 Python 代码。

假设您具有以下结构:

└── my-game
├── app.yaml
└── static
├── index.html
├── js
│   └── script.js
└── res
└── map.txt

app.yaml应该是这样的:

application: my-app
version: 1
runtime: python27
api_version: 1
threadsafe: yes

handlers:

- url: /
static_files: static/index.html
upload: static/index.html

- url: /
static_dir: static/

在您要安装 Google App Engine SDK 之后(如果你还没有这样做),你将能够运行 dev_appserver.py来自终端的命令。如果您有上述结构,请尝试使用以下命令运行它:

$ dev_appserver.py /path/to/my-game

如果一切顺利,您将能够看到您的 index.htmlhttp://localhost:8080 , map.txthttp://localhost:8080/res/map.txt你应该能够弄清楚剩下的。

请注意,您仍然可以使用 python -m SimpleHTTPServer 运行您的应用程序来自 static目录并在 localhost:8000 上测试它.

关于html - 如何在 Google App Engine 上只提供静态文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14683637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com