Google App Engine中的静态页面返回404 [英] Static pages return 404 in Google App Engine

查看:129
本文介绍了Google App Engine中的静态页面返回404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Golang测试Google App Engine SDK,并且遇到了静态html页面的问题。如果我在处理程序下的 app.yaml 中添加内容,但是在尝试从我的Go应用程序中路由它时,试一试url http:// localhost:8080 / tr 页面返回404。



我的文件系统是设置为:

  /main.go 
/app.yaml
/testRoute.html

我的主要 app.go 看起来像这样:

 导入(
fmt
net / http
github.com/gorilla / mux


func init(){
r:= mux.NewRouter()
r.HandleFunc(/,index)
r.HandleFunc(/ tr,testRoute)
http.Handle(/,r)
}

func index(w http.ResponseWriter,r * http。请求){
//这里没有问题
fmt.Fprint(w,Main Index。)
}

func testRoute(w http.ResponseWriter,r * http.Request){
http.FileServer(http.Dir(testRoute.html)).ServeHTTP(w,r)
}
pre

解决方案

您不应该使用Go处理程序来提供静态文件(除非您想要合并其他逻辑su您可以在您的应用程序的配置文件 app.yaml 中定义静态文件处理程序。 。详情请参阅官方文档: 静态文件处理程序


静态文件是直接向用户提供给定URL的文件,例如图像,CSS样式表,或JavaScript源文件。静态文件处理程序描述应用程序目录中的哪些文件是静态文件,以及哪些URL用于服务它们。



为了提高效率,App Engine将应用程序文件与应用程序文件分开存储和提供静态文件。默认情况下,静态文件在应用程序的文件系统中不可用。这可以通过将 application_readable 选项设置为 true 来更改。



静态文件处理程序可以通过两种方式定义:作为静态文件的目录结构映射到URL路径,或作为将URL映射到特定文件的模式。


要使AppEngine自动提供静态文件,请将此条目添加到 app.yaml 中:

   -  url:/ tr 
static_files:testRoute.html
upload:testRoute.html

要自动为静态文件(包括子文件夹,递归)创建一个完整目录,请将此条目添加到 app.yaml

   -  url:/ assets 
static_dir:资产


I've been testing out the Google App Engine SDK using Golang and I'm having issues serving a static html page. If I add the content in the app.yaml under handlers that is fine but when trying to route it from inside my Go application; trying out the url http://localhost:8080/tr the page returns 404.

My file system is setup as:

/main.go
/app.yaml
/testRoute.html

My main app.go looks like this:

import (
    "fmt"
    "net/http"
    "github.com/gorilla/mux"
)

func init() {
    r := mux.NewRouter()
    r.HandleFunc("/", index)
    r.HandleFunc("/tr", testRoute)
    http.Handle("/", r)
}

func index(w http.ResponseWriter, r *http.Request) {
   //No Issues here 
   fmt.Fprint(w, "Main Index.")
}

func testRoute(w http.ResponseWriter, r *http.Request) { 
    http.FileServer(http.Dir("testRoute.html")).ServeHTTP(w, r)
}

解决方案

You shouldn't use Go handlers to serve static files (unless you want to incorporate other logic such as advanced logging or counting).

You may define static file handlers in your app's configuration file app.yaml. This is detailed in the official docs: Static file handlers

Static files are files to be served directly to the user for a given URL, such as images, CSS stylesheets, or JavaScript source files. Static file handlers describe which files in the application directory are static files, and which URLs serve them.

For efficiency, App Engine stores and serves static files separately from application files. Static files are not available in the application's file system by default. This can be changed by setting the application_readable option to true.

Static file handlers can be defined in two ways: as a directory structure of static files that maps to a URL path, or as a pattern that maps URLs to specific files.

To make AppEngine automatically serve a static file, add this entry to your app.yaml:

- url: /tr
  static_files: testRoute.html
  upload: testRoute.html

To make a whole directory of static files (including subfolders, recursively) to be served automatically, add this entry to app.yaml:

- url: /assets
  static_dir: assets

这篇关于Google App Engine中的静态页面返回404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆