静态页面在Google App Engine中返回404错误。

huangapple go评论81阅读模式
英文:

Static pages return 404 in Google App Engine

问题

我一直在使用Golang测试Google App Engine SDK,并且在提供静态HTML页面时遇到了问题。如果我将内容添加到app.yaml的handlers部分,那么一切都正常,但是当我尝试从我的Go应用程序内部路由它时,尝试使用URL http://localhost:8080/tr,页面返回404错误。

我的文件系统设置如下:

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

我的主要app.go代码如下:

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) {
   //这里没有问题
   fmt.Fprint(w, "Main Index.")
}

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

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)
}

答案1

得分: 0

我使用ServeFile方法修复了这个问题;这个方法也适用于文件夹(例如"/assets/testRoute.html")。

func testRoute(w http.ResponseWriter, r *http.Request) { 
    http.ServeFile(w, r, "testRoute.html")
}
英文:

I fixed this by using the ServeFile method; this also works with folders (eg "/assets/testRoute.html")

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

答案2

得分: 0

你不应该使用Go处理程序来提供静态文件(除非你想要加入其他逻辑,如高级日志记录或计数)。

你可以在应用的配置文件app.yaml中定义静态文件处理程序。官方文档详细介绍了这一点:静态文件处理程序

静态文件是直接提供给用户的文件,例如图像、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: assets
英文:

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

huangapple
  • 本文由 发表于 2016年2月26日 05:16:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/35638518.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定