dsh8009271 2018-06-13 22:40
浏览 310
已采纳

golang静态服务器总是返回404页面未找到

I try to get a Go webserver running on a raspberry pi (using 1.10.1) I have a go webserver implemented like (StatPiPrivider.go):

package main

import (
        "net/http"
)

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

and the static folder is in the same folder as the StatPiProvider.go file.

In the folder static/templates are 4 html files including one index.html

Everytime I rewuest the server I get a response 404 page not found. Even if I try to get an other html-file I get the same response.

Is it a problem with my implementation or is something on my raspberry wrong.

I run the code with: go run StatPiProvider/StatPiProvider.go

  • 写回答

1条回答 默认 最新

  • duanna3634 2018-06-13 22:55
    关注

    Note that go run does not change the working directory. So any relative path you use in your app, they will be resolved to the working directory, the folder from which you run go run.

    Since the static folder is next to the StatPiProvider.go file, and you use the path ./static/templates, their containing folder must be the working directory when you run go run.

    So first change dir so the StatPiProvider.go will be in the working directory, and then launch the .go file like this:

    cd StatPiProvider
    go run StatPiProvider.go
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义
  • ¥15 嵌入式--定时器使用