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 CSS实现渐隐虚线边框
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题