你如何使用 go web 服务器提供静态 html 文件? [英] How do you serve a static html file using a go web server?

查看:65
本文介绍了你如何使用 go web 服务器提供静态 html 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何使用 Go 网络服务器提供 index.html(或其他一些静态 HTML 文件)?

How do you serve index.html (or some other static HTML file) using a go web server?

我只想要一个基本的静态 HTML 文件(例如一篇文章),我可以从 Go Web 服务器提供它.HTML 应该可以在 go 程序之外修改,就像在使用 HTML 模板时一样.

I just want a basic, static HTML file (like an article, for example) which I can serve from a go web server. The HTML should be modifiable outside of the go program, as it would be in the case while using HTML templates.

这是我的网络服务器,它只托管硬编码文本(Hello world!").

This is my web server which only hosts hard-coded text ("Hello world!").

package main

import (
  "fmt"
  "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
  fmt.Fprintf(w, "Hello world!")
}

func main() {
  http.HandleFunc("/", handler)
  http.ListenAndServe(":3000", nil)
}

推荐答案

使用 Golang net/http 包,这个任务很容易.

That task is very easy with Golang net/http package.

您需要做的就是:

package main

import (
        "net/http"
)

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

假设静态文件在项目根目录下名为static的文件夹中.

assuming that static files are in folder named static in the root directory of the project.

如果它在文件夹 static 中,您将有 index.html 文件调用 http://localhost:3000/ 这将导致呈现该索引文件而不是列出所有可用的文件.

If it's in folder static, you'll have index.html file calling http://localhost:3000/ which will result in rendering that index file instead of listing all the files availible.

此外,调用该文件夹中的任何其他文件(例如 http://localhost:3000/clients.html)将显示该文件,由浏览器正确呈现(至少是 Chrome、Firefox和 Safari :))

Additionally, calling any other file in that folder (for example http://localhost:3000/clients.html) will show that file, properly rendered by the browser (at least Chrome, Firefox and Safari :))

如果您想提供文件,请从 url 下的文件夹 ./public 说:localhost:3000/static 您必须使用附加功能: func StripPrefix(prefix string, h Handler) Handler 像这样:

If You want to serve files, say from folder ./public under url: localhost:3000/static You have to use additional function: func StripPrefix(prefix string, h Handler) Handler like this:

package main

import (
        "net/http"
)

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

多亏了这一点,您在 ./public 中的所有文件都可以在 localhost:3000/static

Thanks to that, all your files from ./public are avalible under localhost:3000/static

如果没有 http.StripPrefix 函数,如果你尝试访问文件 localhost:3000/static/test.html,服务器会在 中寻找它./public/static/test.html

Without http.StripPrefix function, if you would try to access file localhost:3000/static/test.html, the server would look for it in ./public/static/test.html

这是因为服务器将整个 URI 视为文件的相对路径.

This is because the server treats the whole URI as a relative path to the file.

幸运的是,它可以通过内置函数轻松解决.

Fortunately, it's easily solved with the built-in function.

这篇关于你如何使用 go web 服务器提供静态 html 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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