英文:
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("/", http.FileServer(http.Dir("static")))
http.ListenAndServe(":4747", nil)
}
You don't need `static/home`, just `static`.
[FileServer][1] is using [directory listing][2] and since you don'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'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 (
"github.com/go-martini/martini"
)
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 (
"github.com/go-martini/martini"
)
func main() {
m := martini.Classic()
m.Use(martini.Static("static")) // serve from the "static" 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论