golang 静态服务器总是 returns 找不到 404 页面
golang static server always returns 404 page not found
我尝试在 raspberry pi 上获得一个 Go 网络服务器 运行ning(使用 1.10.1)
我有一个像 (StatPiPrivider.go):
这样实现的网络服务器
package main
import (
"net/http"
)
func main() {
http.Handle("/", http.FileServer(http.Dir("./static/templates")))
http.ListenAndServe(":3000", nil)
}
静态文件夹与 StatPiProvider.go 文件位于同一文件夹中。
文件夹 static/templates 中有 4 个 html 个文件,其中包括一个 index.html
每次我请求服务器时,我都会收到 404 页面未找到的响应。即使我尝试获取另一个 html 文件,我也会得到相同的响应。
是我的实现有问题还是我的树莓派有问题。
I 运行 代码:go 运行 StatPiProvider/StatPiProvider.go
请注意 go run
不会更改工作目录。因此,您在应用程序中使用的任何相对路径都将解析为工作目录,即您 运行 go run
.
所在的文件夹
由于 static
文件夹在 StatPiProvider.go
文件旁边,并且您使用路径 ./static/templates
,因此当您 运行 时,它们所在的文件夹必须是工作目录go run
.
所以首先更改目录,使 StatPiProvider.go
位于工作目录中,然后像这样启动 .go
文件:
cd StatPiProvider
go run StatPiProvider.go
我尝试在 raspberry pi 上获得一个 Go 网络服务器 运行ning(使用 1.10.1) 我有一个像 (StatPiPrivider.go):
这样实现的网络服务器package main
import (
"net/http"
)
func main() {
http.Handle("/", http.FileServer(http.Dir("./static/templates")))
http.ListenAndServe(":3000", nil)
}
静态文件夹与 StatPiProvider.go 文件位于同一文件夹中。
文件夹 static/templates 中有 4 个 html 个文件,其中包括一个 index.html
每次我请求服务器时,我都会收到 404 页面未找到的响应。即使我尝试获取另一个 html 文件,我也会得到相同的响应。
是我的实现有问题还是我的树莓派有问题。
I 运行 代码:go 运行 StatPiProvider/StatPiProvider.go
请注意 go run
不会更改工作目录。因此,您在应用程序中使用的任何相对路径都将解析为工作目录,即您 运行 go run
.
由于 static
文件夹在 StatPiProvider.go
文件旁边,并且您使用路径 ./static/templates
,因此当您 运行 时,它们所在的文件夹必须是工作目录go run
.
所以首先更改目录,使 StatPiProvider.go
位于工作目录中,然后像这样启动 .go
文件:
cd StatPiProvider
go run StatPiProvider.go