GO: 提供静态页面服务

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

GO: Serve static pages

问题

我正在尝试使用GO显示一个静态页面。

GO代码:

package main

import "net/http"

func main() {
    fs := http.FileServer(http.Dir("static/home"))
    http.Handle("/", fs)
    http.ListenAndServe(":4747", nil) 
}

目录结构:

Project
    /static
        home.html
        edit.html
    project.go

当我运行它时,网页显示的是指向edit.html和home.html的链接,而不是显示来自home.html的静态页面。我做错了什么?这是提供文件的最佳方式吗?我知道还有其他方法,例如使用html/templates包,但我不确定它们之间的区别以及何时使用每种方法。谢谢!

英文:

I'm trying to display a static page with GO.

GO:

package main

import "net/http"

func main() {
    fs := http.FileServer(http.Dir("static/home"))
    http.Handle("/", fs)
    http.ListenAndServe(":4747", nil) 
}

Directory:

Project
    /static
        home.html
        edit.html
    project.go

When I run it, the web page displays the links to edit.html and home.html instead of displaying the static page from home.html. What am I doing wrong. Is this the best way to serve files? I know there are other ways eg. from the html/templates package but I'm not sure what the difference is and when to use each of these methods. Thanks!

答案1

得分: 2

主函数如下所示:

func main() {
    http.Handle("/", http.FileServer(http.Dir("static")))
    http.ListenAndServe(":4747", nil)
}

你不需要使用 static/home,只需要使用 static

FileServer 使用 directory listing,由于 /static 目录下没有 index.html 文件,所以会显示目录内容。

一个快速的解决方法是将 home.html 重命名为 index.html。这样你就可以通过 http://localhost:4747/ 访问 index.html,通过 http://localhost:4747/edit.html 访问 edit.html

如果你只需要提供静态文件,就不需要使用 html/template

但是一个清晰的解决方案取决于你实际想要做什么。


<details>
<summary>英文:</summary>

	func main() {
		http.Handle(&quot;/&quot;, http.FileServer(http.Dir(&quot;static&quot;)))
		http.ListenAndServe(&quot;:4747&quot;, nil)
	}

You don&#39;t need `static/home`, just `static`.

[FileServer][1] is using [directory listing][2] and since you don&#39;t have an `index.html` in `/static`, the directory content is shown instead.

A quick fix would be to just rename `home.html` to `index.html`. This would allow you to access `index.html` through `http://localhost:4747/` and `edit.html` with `http://localhost:4747/edit.html`.

There&#39;s no need to use `html/template` if you only need to serve static files.

But a clean solution depends on what you are actually trying to do.

  [1]: http://golang.org/pkg/net/http/#FileServer
  [2]: http://golang.org/src/pkg/net/http/fs.go#L66

</details>



# 答案2
**得分**: 1

如果你只对编写一个简单的服务器来提供静态内容感兴趣,并且不仅仅是为了学习的目的,我建议你看一下Martini(http://martini.codegangsta.io/)。

一个典型的Martini应用程序用于从名为'public'的文件夹中提供静态文件的代码如下:

```go
package main

import (
	"github.com/go-martini/martini"
)

func main() {
	m := martini.Classic()
	m.Run()
}

要将一个名为'static'的新静态文件夹添加到搜索内容的静态文件夹列表中也很简单:

package main

import (
	"github.com/go-martini/martini"
)

func main() {
	m := martini.Classic()
	m.Use(martini.Static("static")) // 也从"static"目录提供静态文件
	m.Run()
}

Martini还提供了更多功能,如会话管理、模板渲染、路由处理等等。

我们在生产环境中使用Martini,并对它及其周边基础设施非常满意。

英文:

If you're only interested in writing a simple server that serves static content and not just as a learning experience I'd take a look at Martini (http://martini.codegangsta.io/).

The quintessential Martini app to serve static files from a folder named 'public' would be:

package main

import (
	&quot;github.com/go-martini/martini&quot;
)

func main() {
	m := martini.Classic()
    m.Run()
}

to add a new static folder called 'static' to the list of static folders searched for content is also simple:

package main

import (
	&quot;github.com/go-martini/martini&quot;
)

func main() {
	m := martini.Classic()
	m.Use(martini.Static(&quot;static&quot;)) // serve from the &quot;static&quot; directory as well
	m.Run()
}

Martini provides lots more functionality as well, such as session, template rendering, route handlers etc...

We're using Martini in production here and have been more than happy with it and it's surrounding infrastructure.

huangapple
  • 本文由 发表于 2014年7月27日 20:25:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/24980927.html
匿名

发表评论

匿名网友

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

确定