2014-01-15 3 views
1
import (
    "net/http" 
) 

func main() { 
    http.Handle("/", http.FileServer(http.Dir("static"))) 
    http.ListenAndServe(":8080", nil) 
} 

나는 localhost : 8080 /으로 이동하고 404 오류가 발생합니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?파일 시스템에서 파일을 제공하려고 시도 할 때

+0

GET /?에서 무엇을 기대 하시겠습니까? – Volker

+0

정적 폴더의 My Index.html – Dante

+0

'server.RegisterHandlers()는 무엇을합니까? "정적"이라는 폴더가 있으면 코드가 제대로 작동합니다. – ANisus

답변

2

이 시도 :

import (
    "net/http" 
    "os" 
) 

func main() { 
    server.RegisterHandlers() 

    // Get the running directory. 
    runningDirectory, err := os.Getwd() 
    if err != nil { 
     // Handle the error. 
     return err 
    } 

    http.Handle("/", http.FileServer(http.Dir(runningDirectory + "/static")) 
    http.ListenAndServe(":8080", nil) 
} 
+0

잘 작동했습니다! 일반적으로 친척이 충분할 때 전체 경로가 필요한 이유를 알고 있습니까? – ANisus

0

을 다음과 같은 구조 :

main.go 
static 
    |- index.html 

main.go이 함유 :

package main 

import (
    "net/http" 
) 

func main() { 
    http.Handle("/", http.FileServer(http.Dir("static"))) 
    if err := http.ListenAndServe(":8080", nil); err != nil { 
     panic(err) 
    } 
} 

go run main.go과 솔루션을 실행 한 후, 당신은 할 수 있어야 localhost:8080/으로 이동하여 0123의 콘텐츠로 끝납니다..

작동하지 않는 경우 추가 된 오류 처리가 도움이 될 수 있습니다. 코드가 정확합니다.

관련 문제