404在heroku上部署Go服务器并呈现后未找到页面

btxsgosb  于 2022-11-13  发布在  Go
关注(0)|答案(1)|浏览(142)

在将我的应用程序部署到Heroku后,我得到的只是一个404页未找到的错误。在我的本地机器上,它工作得很好。

package main

import (
    "encoding/json"
    "fmt"
    "log"
    "net/http"
    "os"
)

type helloHngResponse struct{
    SlackUsername string `json:"SlackUsername"`
    Backend bool `json:"Backend"`
    Age int `json:"Age"`
    Bio string `json:"Bio"`
}

func helloHng(w http.ResponseWriter, r *http.Request){
    response := helloHngResponse{SlackUsername: "kodeforce98", Backend: true, Age: 24, Bio: "Proud firstborn, Golang Developer, Committed christian, Faithful boyfriend"}
    
    encoder := json.NewEncoder(w)
    encoder.Encode(response)
}

我觉得罪犯就在附近,但我还是迷路了。

func main(){

    port := os.Getenv("PORT")
    if port == ""{
        port = "9090"
    }
         
     
    http.HandleFunc("/hellohng", helloHng)
    log.Printf("Server starting on port %v\n", port)
    log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", port), nil))
}

我试着理解为什么我在本地运行json时没有得到json输出。感谢所有帮助

8e2ybdfx

8e2ybdfx1#

有人猜测:

*您可能根本没有部署-您是否遵循以下部署指南:https://devcenter.heroku.com/articles/getting-started-with-go?singlepage=true-如果是,您是否在服务器日志中看到404?
*您发送请求时没有使用端口-您是否将请求发送到正确的端口?https://polar-inlet-4930.herokuapp.com:9000/hellohng。您还可以在代码中将端口从9000更改为80,这可能会有所帮助,因为80是Web请求的默认端口。

相关问题