dongpengqin3898 2014-09-24 12:52
浏览 74
已采纳

使用Go net / http服务链接的CSS和js

I cannot make net/http serve html file with the linked css and script file.

I have

site/lib/ratchet/css/ratchet.css
site/lib/ratchet/js/ratchet.js

in my project folder structure, and

site/src/index.html

and in this index.html I have included two files

<link href="../lib/ratchet/css/ratchet.css" rel="stylesheet">

<script src="../lib/ratchet/js/ratchet.js"></script>

and Go function to serve it is:

func index(w http.ResponseWriter, r *http.Request) {
    http.ServeFile(w, r, "/Users/faruk/dev/otp/site/src/index.html")
}

in main():

r.HandleFunc("/", index).
    Methods("GET")

I make use of gorilla/mux above.

I can view it from the browser but with only html. the two linked file has been given 404.

Can not http.ServeFile intended for this purpose to automatically resolve the linked css and js files to serve the index.html or alike files? What is the standard way to serve an html file in Go net/http?

  • 写回答

1条回答 默认 最新

  • doumou1864 2014-09-24 13:54
    关注

    You need to setup a file server to serve the files found in your lib directory. Go has a file server in the http package

    func init() {
        http.Handle("/lib/", http.FileServer(http.Dir("/Users/faruk/dev/otp/site/")))
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 pycharm倒入虚拟环境的时候,显示这个,但是我的虚拟环境已经创建了
  • ¥15 FPGA芯片60进制计数器
  • ¥15 前端js怎么实现word的.doc后缀文件在线预览
  • ¥20 macmin m 4连接iPad
  • ¥15 DBIF_REPO_SQL_ERROR
  • ¥15 根据历年月数据,用Stata预测未来六个月汇率
  • ¥15 DevEco studio开发工具 真机联调找不到手机设备
  • ¥15 请教前后端分离的问题
  • ¥100 冷钱包突然失效,急寻解决方案
  • ¥15 下载honeyd时报错 configure: error: you need to instal a more recent version of libdnet
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部