dongsang6899 2014-01-20 12:50
浏览 69
已采纳

Golang Gorilla Mux与http.FileServer返回404

The problem I'm seeing is that I'm trying to use the http.FileServer with the Gorilla mux Router.Handle function.

This doesn't work (the image returns a 404)..

myRouter := mux.NewRouter()
myRouter.Handle("/images/", http.StripPrefix("/images/", http.FileServer(http.Dir(HomeFolder + "images/"))))

this works (the image is shown ok)..

http.Handle("/images/", http.StripPrefix("/images/", http.FileServer(http.Dir(HomeFolder + "images/"))))

Simple go web server program below, showing the problem...

package main

import (
    "fmt"
    "net/http"
    "io"
    "log"
    "github.com/gorilla/mux"
)

const (
    HomeFolder = "/root/test/"
)

func HomeHandler(w http.ResponseWriter, req *http.Request) {
    io.WriteString(w, htmlContents)
}

func main() {

    myRouter := mux.NewRouter()
    myRouter.HandleFunc("/", HomeHandler)
    //
    // The next line, the image route handler results in 
    // the test.png image returning a 404.
    // myRouter.Handle("/images/", http.StripPrefix("/images/", http.FileServer(http.Dir(HomeFolder + "images/"))))
    //
    myRouter.Host("mydomain.com")
    http.Handle("/", myRouter)

    // This method of setting the image route handler works fine.
    // test.png is shown ok.
    http.Handle("/images/", http.StripPrefix("/images/", http.FileServer(http.Dir(HomeFolder + "images/"))))

    // HTTP - port 80
    err := http.ListenAndServe(":80", nil)

    if err != nil {
        log.Fatal("ListenAndServe: ", err)
        fmt.Printf("ListenAndServe:%s
", err.Error())
    }
}

const htmlContents = `<!DOCTYPE HTML>
<html lang="en">
  <head>
    <title>Test page</title>
    <meta charset = "UTF-8" />
  </head>
  <body>
    <p align="center">
        <img src="/images/test.png" height="640" width="480">
    </p>
  </body>
</html>
`
  • 写回答

2条回答 默认 最新

  • douji8033 2014-01-21 07:20
    关注

    I posted this on golang-nuts discussion group and got this solution from Toni Cárdenas ...

    The standard net/http ServeMux (which is the standard handler you are using when you use http.Handle) and the mux Router have different ways of matching an address.

    See the differences between http://golang.org/pkg/net/http/#ServeMux and http://godoc.org/github.com/gorilla/mux.

    So basically, http.Handle('/images/', ...) matches '/images/whatever', while myRouter.Handle('/images/', ...) only matches '/images/', and if you want to handle '/images/whatever', you have to ...

    1. set a regular expression match in your router or
    2. use the PathPrefix method on your router, like:

    Code examples

    1.

    myRouter.Handle('/images/{rest}', 
        http.StripPrefix("/images/", http.FileServer(http.Dir(HomeFolder + "images/")))
    )
    

    2.

    myRouter.PathPrefix("/images/").Handler(
        http.StripPrefix("/images/", http.FileServer(http.Dir(HomeFolder + "images/")))
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

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