Search code examples
httpgowebserverraspberry-pi3

golang static server always returns 404 page not found


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


Solution

  • 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