1

我正在尝试将我的应用程序放在 Heroku 上。我在前端使用 angular,在后端使用 Go。

我按照本教程http://mmcgrana.github.io/2012/09/getting-started-with-go-on-heroku.html

但是,当我转到我的 heroku 应用程序的域时,我得到了我的应用程序的目录(git 中的所有内容)。当我导航到 /app 文件夹(我的 Angular 应用程序所在的位置)时,它会显示该应用程序。

我不希望我的应用在

foobar.herokuapp.com/app/#/

我希望它在

foobar.herokuapp.com

我的应用程序目录的简化版本是:

foobar
 - /app
 - /server/server.go
 - .godir             // contains "app"
 - Procfile           // contains "web: server"

我从 /server 文件夹中运行“go get”

这些工作:

$ PORT=5000 demoapp

$ curl -i http://127.0.0.1:5000/

这是我的简单 server.go

package main

import (
"github.com/gorilla/handlers"
"log"
"net/http"
"os"
)

func main() {
log.Println("Starting Server")
http.Handle("/", logHandler(http.FileServer(http.Dir("../app/"))))

log.Println("Listening...")
panic(http.ListenAndServe(":"+os.Getenv("PORT"), nil))
}

func logHandler(h http.Handler) http.Handler {
return handlers.LoggingHandler(os.Stdout, h)

}

4

1 回答 1

1

将 FileServer 目录从(relative) 或(absolute)更改"../app/"为应该可以解决问题。"app/""/app/app/"

http.Handle("/", logHandler(http.FileServer(http.Dir("app/"))))

当 Heroku 执行 Procfile 命令时,您的项目根目录是工作文件夹。它具有绝对路径/app,这就是为什么../app带您回到项目根目录的原因。

尽管您server.go存储在./server子文件夹中,但它仍会使用package main.

于 2014-01-10T14:26:42.707 回答