为了账号安全,请及时绑定邮箱和手机立即绑定

卷曲 http 服务器没问题,但浏览器不起作用

卷曲 http 服务器没问题,但浏览器不起作用

Go
湖上湖 2023-06-01 18:14:13
我只是在 Golang 中接近 webapps。这是作为起点的简单代码:package mainimport (        "fmt"        "log"        "net/http")const (        CONN_HOST = "localhost"        CONN_PORT = "8080")func helloWorld(w http.ResponseWriter, r *http.Request) {        fmt.Fprintf(w, "Hello World!")}func main() {        http.HandleFunc("/", helloWorld)        err := http.ListenAndServe(CONN_HOST+":"+CONN_PORT, nil)        if err != nil {                log.Fatal("error starting http server : ", err)                return        }}执行:go run http-server.gocurl http://localhost:8080/Hello World!但是在网络浏览器中打开 ip 地址时:http://111.111.1.1:8080/connection didn't succeed如果我替换这段代码:        err := http.ListenAndServe(CONN_HOST+":"+CONN_PORT, nil)        if err != nil {                log.Fatal("error starting http server : ", err)                return        }和 :         log.Fatal(http.ListenAndServe(":8080", nil))所以 main() 函数仅由这两行组成:    func main() {        http.HandleFunc("/", helloWorld)    }curl http://localhost:8080/Hello World!在网络浏览器中:http://111.111.1.1:8080/Hello World!那么....如何使原始的简单 http-server.go 在网络浏览器中工作,而不仅仅是使用命令行 curl ?期待您的帮助。马可
查看完整描述

1 回答

?
慕姐8265434

TA贡献1813条经验 获得超2个赞

你的服务器监听的ip地址是localhost,所以它只处理请求到localhost

你可以尝试curl http://111.111.1.1:8080/,你也会失败。

如果你想从局域网或任何其他 IP 访问你的服务器,你应该设置CONN_HOST = "111.111.1.1"。


查看完整回答
反对 回复 2023-06-01
  • 1 回答
  • 0 关注
  • 73 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信