【问题标题】:Not able to access static webpage index.html via martini framework无法通过 martini 框架访问静态网页 index.html
【发布时间】:2014-07-26 19:37:53
【问题描述】:

您好,我正在尝试通过 martini 框架访问和显示静态 index.html 页面。但我总是收到 404 not found 错误。 .html 文件位于 public/index.html 中,其中 /public 目录位于我的 go/src/github.com/user/ 目录中。 我能够显示Hello World!通过代码通过马提尼 -

    package main

    // loading in the Martini package
    import "github.com/codegangsta/martini"

    func main() {
      // if you are new to Go the := is a short variable declaration
      m := martini.Classic()

      // the func() call is creating an anonymous function that retuns a stringa
      m.Get("/", func() string {
        return "Hello World !!"
      })

      m.Run()

}

所以我确信马提尼酒的配置是正确的。但是当我尝试通过 -

访问静态网页时
package main

import (
        "github.com/codegangsta/martini"
        //"log"
        //"net/http"

)

func main() {
        m := martini.Classic()
        m.Run()
}

我刚刚在 localhot:3000 上得到 404。 任何帮助我如何访问 html 文件?

PS - 我正在尝试做类似这里提到的事情 - https://gophercasts.io/lessons/3-martini-and-markdown

编辑 - 使用m.Use(martini.Static("C:/Users/shrinr/go_projects/go/bin/public")) 也没有帮助,我的 $GOPATH 是 C:/Users/shrinr/go_projects/go。

【问题讨论】:

  • 使用 golang.org/pkg/os/#Getwd 并打印返回的值 (log.Println)。我们可以安全地假设您也没有使用go run 吗?
  • 是的,我正在使用go run main.go 命令来启动/部署 .go 文件。

标签: html go martini


【解决方案1】:

假设您正在使用 Go 的标准过程(在 $GOPATH 中)编译程序,问题是您的二进制文件与 public/ 文件夹不在同一目录中。你应该把它放在$GOPATH/bin 文件夹中(你的二进制文件应该放在哪里)。

【讨论】:

  • 嗨,我再次尝试将我的 public/ 文件夹移动到 $GOPATH\bin 文件夹,但同样的问题仍然存在。我的 $GOPATH 变量设置为 C:\Users\usrname\go_projects\go
  • 您是否尝试访问 http://localhost:3000http://localhost:3000/index.html ?我不认为马提尼酒处理自动索引......
猜你喜欢
  • 2020-11-10
  • 2018-06-11
  • 2012-10-07
  • 1970-01-01
  • 1970-01-01
  • 2011-04-17
  • 1970-01-01
  • 2017-07-24
  • 2021-06-16
相关资源
最近更新 更多