- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我是Go语言的初学者。我试图用 Gorrila Mux 路由器提供静态容器。但是 css 和 js 不是我的服务器。
project
f-mymux.go
d-pages
f-home.html
f-about.html
d-public
d-css
f-style.css
d-js
f-script.js
注意:f-文件&d-目录
我的GO代码如下:
package main
import (
"bufio"
"github.com/gorilla/mux"
"log"
"net/http"
"os"
"strings"
"text/template"
)
func main() {
serverWeb()
}
var staticPages = populateStaticPages()
func serverWeb() {
gorillaRoute := mux.NewRouter().StrictSlash(true)
gorillaRoute.HandleFunc("/", serveContent)
gorillaRoute.HandleFunc("/{page_alias}", serveContent)
gorillaRoute.HandleFunc("/css", serveResource)
port := ":8080"
log.Println("Listening at port :", port)
http.Handle("/", gorillaRoute)
err := http.ListenAndServe(port, nil)
if err == nil {
log.Fatal(err)
}
}
func serveContent(w http.ResponseWriter, r *http.Request) {
pathX := r.URL.Path
log.Println(pathX)
urlParams := mux.Vars(r)
page_alias := urlParams["page_alias"]
if page_alias == "" {
page_alias = "home"
}
staticPage := staticPages.Lookup(page_alias + ".html")
if staticPage == nil {
staticPage = staticPages.Lookup("404.html")
w.WriteHeader(404)
}
staticPage.Execute(w, nil)
}
func populateStaticPages() *template.Template {
result := template.New("template")
templatePaths := new([]string)
basePath := "pages"
templateFolder, _ := os.Open(basePath)
defer templateFolder.Close()
templatePathsRow, _ := templateFolder.Readdir(-1)
for _, pathInfo := range templatePathsRow {
log.Println(pathInfo.Name())
*templatePaths = append(*templatePaths, basePath+"/"+
pathInfo.Name())
}
result.ParseFiles(*templatePaths...)
return result
}
//---------------------------------------------
// Serve resource of types css, js & img files
//---------------------------------------------
func serveResource(w http.ResponseWriter, r *http.Request) {
path := "./public" + r.URL.Path
var contentType string
if strings.HasSuffix(path, ".css") {
contentType = "text/css; charset=utf-8"
} /* else if strings.HasSuffix(path , ".png"){
contentType = "image/png; charset=utf-8"
} else if strings.HasSuffix(path , ".jpg"){
contentType = "image/jpg; charset=utf-8"
} else if strings.HasSuffix(path , ".js"){
contentType = "application/javascript; charset=utf-8"
} else {
contentType = "text/plain; charset=utf-8"
}*/
f, err := os.Open(path)
if err == nil {
defer f.Close()
w.Header().Add("Content-Type", contentType)
br := bufio.NewReader(f)
br.WriteTo(w)
} else {
w.WriteHeader(404)
}
}
当我调用代码时 http://localhost:8080/home然后页面没有 css 和 js。调用页面时 http://localhost:8080/css/bootstrap.min.css然后 404 状态码来了
请帮助我这里做错了什么。我们可以在 Java 和 Java 服务器上轻松完成。但是在Go语言上我花了一整天但是无法解决问题。你的帮助是适当的。
提前致谢。
最佳答案
正如 mkopriva 所述,http.FileServer
是必经之路。您的公用文件夹可以位于您希望的任何位置或任何位置,只要它作为 http.FileServer
参数正确引用即可。
添加这个会起作用:
fs := http.FileServer(http.Dir("./public"))
gorillaRoute.PathPrefix("/js/").Handler(fs)
gorillaRoute.PathPrefix("/css/").Handler(fs)
这样,对 http://[host]:[port]/css/style.css
的 GET
请求将返回 style.css
来自相对 ./public/css/
目录。
关于go - Gorilla mux 路由器不提供静态内容——比如 style.css、script.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46515542/
这两个文件之间的主要区别是什么:styles.xml (res\values\styles.xml) 和 styles.xml (res\values-v21\styles.xml ? 针对旧的and
我正在尝试将按钮样式设置为看起来像我在 Android Full Width ICS style Minimalist Bottom ButtonsViews 中询问的那些按钮. 我已经成功了,有兴趣
只是想检查一下: 如果我有 Style.css 和 Style.min.css(在同一目录中)并且我的 html 页面引用了 Style.css,浏览器/服务器是否会下载 Style.min.css?
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
从上面的问题,我认为这会相对容易,但我找不到任何关于如何向“样式”下拉菜单添加样式的文档。谁能把我推向正确的方向? 最佳答案 样式下拉列表会根据主题的typography.css 文件中的类自动填充。
我有两种风格 还有这个 如果我尝试在这样的对象上使用第二个 编译器抛出这个错误: 错误 16 Style 对象不能影响它所应用的对象的 St
我想知道是否有关于在 Lisp 中使用标签的标准做法。我一直在弄乱这里第一个答案中描述的算法的 Lisp 实现 Generating permutations lazily我当前的版本使用标签来分解部
我想以编程方式获取样式为“ButtonBar”的 LinearLayout 的背景颜色。 我试过用LinearLayout的getBackgroundColor,没找到方法。 有人有想法吗?问候 最
我在扩展 javax.swing.text.DefaultStyledDocument 的类中遇到间歇性问题。该文档正在发送到打印机。大多数情况下,文档的格式看起来是正确的,但有时却并非如此。看起来格
我想将所有元素设为边框。我想这样做: * { box-sizing: border-box; } 如何使用 React 的内联样式做到这一点?我不想在每个组件中都写这个规则... 最佳答案 这是不
当我创建一个 Android 应用程序项目时,我在 (android:theme="@style/AppTheme") 上的 AndroidManifest.xml 中出现错误 找不到与给定名称匹配的
一种风格ol.layer.Vector可以设置为 ol.style.Style ,样式函数或 ol.style.Style 的数组.数组的用途和作用——与仅传递 ol.style.Style 相比目的
我的意思是内部风格 #div {color:red;} document.getElementsByTagName('style').innerHTML 不工作... document.style
http://synergine.net/rain.php 你好。我试图清除 .ripple div 中所有元素的样式属性,但没有成功: function contact(level){ focus_
我使用 vue 和 v-for 循环来创建跨度。以下是使用 bootstrap4 的背景颜色的一种样式的成功: {{ group }} export default {
有没有办法只存储元素的当前样式状态,这样我就可以搞砸样式然后再重置它? 类似于(虽然这不起作用):http://jsfiddle.net/843Pj/ var el=document.getEleme
我正在尝试将 tinymce 配置为不允许在 style 属性中使用 css 样式。 我只想允许一种样式,即文本装饰。这是一个类似的问题 http://tinymce.moxiecode.com/pu
我对style.css做了一些修改,上传到网上。但是它没有显示我需要的结果。即它仍然采用旧的 style.css 代码。 我可以离线查看更改,但是当我给它完整的 href 链接时,它没有显示必要的结果
我添加到 web 文件夹下的样式文件夹似乎没有被我的 JSP 上的调度程序 servlet 映射。我明白了 WARN : org.springframework.web.servlet.PageNot
是否有任何用于 JQuery 数据表的 Metro Style CSS 样式插件? 最佳答案 看看here 或者您可以自己创建一个。 Metro 风格很容易用 Segoe 字体复制 关于jquery
我是一名优秀的程序员,十分优秀!