dps69208 2019-02-14 12:52
浏览 309

Golang在服务器运行时获取404

I am trying to run a basic web app, following a tutorial, using Golang and the routing package Gorilla/mux. The server is running fine but it refuses to find the index.html file regardless of what I put in the browser, always returning a 404.

Here is the code:

main.go

    package main

    import (
    "database/sql"
    "fmt"
    "net/http"

    "github.com/gorilla/mux"
    _ "github.com/lib/pq"
    )

    const (
    host     = "localhost"
    port     = 5432
    user     = "postgres"
    password = "0102"
    dbname   = "bird_encyclopaedia"
)

func newRouter() *mux.Router {
    r := mux.NewRouter()
    r.HandleFunc("/hello", handler).Methods("GET")


    staticFileDirectory := http.Dir("./assets/")
    staticFileHandler := http.StripPrefix("/assets/", http.FileServer(staticFileDirectory))
    r.PathPrefix("/assets/").Handler(staticFileHandler).Methods("GET")

    r.HandleFunc("/bird", getBirdHandler).Methods("GET")
    r.HandleFunc("/bird", createBirdHandler).Methods("POST")
    return r
}

func main() {
    fmt.Println("Starting server dickface...")
    connString := fmt.Sprintf("host=%s port=%d user=%s "+
        "password=%s dbname=%s sslmode=disable",
        host, port, user, password, dbname)
    db, err := sql.Open("postgres", connString)

    if err != nil {
        panic(err)
    }
    err = db.Ping()

    if err != nil {
        panic(err)
    }

    InitStore(&dbStore{db: db})

    r := newRouter()
    fmt.Println("Serving on port 8080")
    http.ListenAndServe(":8080", r)
}

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

The html file is just in the assets/index.html directory, I can supply that if need be but I can't see the problem being in the actual html?

I have trawled through the code many times and cannot see why the server would not be able to find the directory. I have tried localhost/8080/assets, localhost/8080/assets/index.html, localhost/8080, and all other variants.

If I append it with /hello mind it returns the Hello world as seen in main.go And if I append it with /bird it returns "null" instead of 404.

  • 写回答

1条回答 默认 最新

  • dsag14654 2019-02-14 13:45
    关注

    You don't need http.StripPrefix() since you are not using the assets in the URL.


    Just change these two lines:

    staticFileHandler := http.StripPrefix("/assets/", http.FileServer(staticFileDirectory))
    r.PathPrefix("/assets/").Handler(staticFileHandler).Methods("GET")
    

    to

    staticFileHandler := http.FileServer(staticFileDirectory)
    r.PathPrefix("/").Handler(staticFileHandler).Methods("GET")
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据