dta43039 2014-07-20 11:04
浏览 33
已采纳

第三方路由器和静态文件

I'm using a third-party router (httprouter) on Google App Engine and would like to serve static files from root.

Because of App Engine, I need to attach the third-party router to the DefaultServeMux on /:

router := httprouter.New()

// Doesn't work, duplicated "/".
http.Handle("/", http.FileServer(http.Dir("public")))

// Needed because of App Engine.
http.Handle("/", router)

The problem is this duplicates the / pattern and panics with "multiple registrations for /"

How can I serve files, especially index.html from root and use a third-party router?

  • 写回答

1条回答 默认 最新

  • dssjxvbv918586 2014-07-20 12:01
    关注

    If you serve static files at / then you can't serve any other paths as per https://github.com/julienschmidt/httprouter/issues/7#issuecomment-45725482

    You can't register a "catch all" at the root dir for serving files while also registering other handlers at sub-paths. See also the note at https://github.com/julienschmidt/httprouter#named-parameters

    You should use Go to serve a template at the application root and static files (CSS, JS, etc) at a sub path:

    router := httprouter.New()
    
    router.GET("/", IndexHandler)
    // Ripped straight from the httprouter docs
    router.ServeFiles("/static/*filepath", http.Dir("/srv/www/public/"))
    
    http.Handle("/", router)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?