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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?