go - Httprouter trouble with static files -


i'm using router (httprouter) , serve static files root.

css file in

static/style.css

in template

<link href="./static/style.css" rel="stylesheet">

main.go

router := httprouter.new() router.servefiles("/static/*filepath", http.dir("/static/")) router.get("/", index) 

but http://localhost:3001/static/style.css gives me 404 error , style in render page doesn't work too.

try replace http.dir("/static/") http.dir("static") (which relative path static dir) or http.dir("/absolute/path/to/static"). example single change works me.

also see httprouter's servefiles documentation:

func (r *router) servefiles(path string, root http.filesystem)

servefiles serves files given file system root. path must end "/*filepath", files served local path /defined/root/dir/*filepath. example if root "/etc" , *filepath "passwd", local file "/etc/passwd" served. internally http.fileserver used, therefore http.notfound used instead of router's notfound handler. use operating system's file system implementation, use http.dir:

router.servefiles("/src/*filepath", http.dir("/var/www"))

this might of - third-party router , static files

i must admit it's unclear me why 'static' needed twice. if set http.dir "." works single difference need navigate localhost:3001/static/static/style.css


Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -