0

I am new to GoLang dev and am trying to make a simple web-app. I have been following this tutorial https://www.youtube.com/watch?v=AiRhWG-2nGU.

However I can not even serve the index.html file.

This is my code

func check(e error) {
    if e != nil {
        fmt.Println(e)
        panic(e)
    }
}

func Index(w http.ResponseWriter, r *http.Request) {
    fmt.Println("Index functoin")
    indexHTML, err := ioutil.ReadFile("index.html")
    check(err)
    fmt.Println(indexHTML)
    w.Write(indexHTML)
}

and this is the error produced

Index functoin
open index.html: no such file or directory

My tree structure is like so

BasicWebServer/
BasicWebServer/main.go
BasicWebServer/index.html
BasicWebServer/static/
BasicWebServer/static/index.html

All I want is to be able to serve the index.html since it is a AngularJS app which is already running smoothly. I tried with static files like so

router := NewRouter()
s := http.StripPrefix("/static/", http.FileServer(http.Dir("./static/")))

but it did not work so I am now trying the most basic approach I could think of.

Please help.

Thank you

mp3por
  • 1,796
  • 3
  • 23
  • 35
  • Your files need to be relative to the working directory of your server process. The location in the source has nothing to do with where you run the server. – JimB Jun 22 '16 at 17:27
  • 2
    Possible duplicate of [404 page not found - Go rendering css file](http://stackoverflow.com/questions/28293452/404-page-not-found-go-rendering-css-file) – icza Jun 22 '16 at 17:29
  • 2
    Also related / possible duplicate of [Why do I need to use http.StripPrefix to access my static files?](http://stackoverflow.com/questions/27945310/why-do-i-need-to-use-http-stripprefix-to-access-my-static-files) – icza Jun 22 '16 at 17:31
  • Guys this has nothing to do with the problem that I can not read a file correctly from the FileSystem – mp3por Jun 23 '16 at 12:28

1 Answers1

3

If you want BasicWebServer/main.go to show BasicWebServer/index.html, not the one inside the static folder, then it seems you didn't properly configure the HTTP server.

Here's your code, with package declaration, imports and a main function working as you expected.

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
)

func check(e error) {
    if e != nil {
        fmt.Println(e)
        panic(e)
    }
}

func Index(w http.ResponseWriter, r *http.Request) {
    fmt.Println("Index functoin")
    indexHTML, err := ioutil.ReadFile("index.html")
    check(err)
    fmt.Println(indexHTML)
    w.Write(indexHTML)
}

func main() {
    http.HandleFunc("/", Index)

    err := http.ListenAndServe(":8080", nil)
    check(err)
}
toqueteos
  • 771
  • 1
  • 6
  • 14
  • Hey, man. Thank you for the answer. I just tried it and it did not work. I get " The localhost page isn’t working localhost didn’t send any data. ERR_EMPTY_RESPONSE " again. What i did was to copy your source, paste it in a new file main2.go, and run " go run BasicWebServer/main2.go ". The index.html file is in the same dir as main2.go .... – mp3por Jun 23 '16 at 12:34
  • Index functoin22222 open index.html: no such file or directory .... This is the output in the console – mp3por Jun 23 '16 at 12:36
  • If i supply the full path I get the correct file though. However I do not want to have to supply the full path since this means all static imports will be bound to my set-up. What can we do about this ? – mp3por Jun 23 '16 at 12:42
  • There's `http.StripPrefix` for that. You shouldn't have such problems with relative paths. Let me give it a try and I'll link you an example. – toqueteos Jun 23 '16 at 12:55
  • Also, I see you run the application using `go run BasicWebServer/main2.go` this means your current working directory is different to the one you are using in your application. That's something we can work with too. – toqueteos Jun 23 '16 at 12:57
  • I have no idea man. I literally started this week so everything is new to me :D THanks for the help – mp3por Jun 23 '16 at 12:59
  • 1
    Go inside `BasicWebServer` directory, then `go run main2.go`, your relative path will work just fine. – toqueteos Jun 23 '16 at 12:59
  • 1
    Yes it does. Thank you. Any comment on what is going on. It feels kind of strange to run the app from within its inner folders ... Also my static files are still not showing up :X I can make a separate question for this if needed – mp3por Jun 23 '16 at 13:02
  • I will make another question for the static files after I give it another try :D sorry to bother you. – mp3por Jun 23 '16 at 13:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/115409/discussion-between-toqueteos-and-mp3por). – toqueteos Jun 23 '16 at 13:04