英文:
Point domain to go server with gorilla mux
问题
我有一个小型服务器,我希望该服务器使用gorilla/mux包监听我的自定义域名sftablet.dev。
以下是代码:
package main
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
)
func main() {
r := mux.NewRouter()
r.Host("sftablet.dev")
r.HandleFunc("/", HomeHandler)
r.HandleFunc("/products", ProductsHandler)
http.ListenAndServe(":8080", r)
}
func HomeHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hey, this is homepage")
}
func ProductsHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hey, this is products")
}
我还在hosts文件中添加了以下内容:
127.0.0.1 sftablet.dev
但出于某种原因它不起作用。如果我访问127.0.0.1:8080,它可以正常工作,但是当我访问http://sftablet.dev/时却不行。我也清除了DNS缓存。
英文:
I have a small server and I want that server to listen to my custom domain sftablet.dev using gorilla/mux package.
Here is the code:
package main
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
)
func main() {
r := mux.NewRouter()
r.Host("sftablet.dev")
r.HandleFunc("/", HomeHandler)
r.HandleFunc("/products", ProductsHandler)
http.ListenAndServe(":8080", r)
}
func HomeHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hey, this is homepage")
}
func ProductsHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hey, this is products")
}
I also added this in the hosts file:
127.0.0.1 sftablet.dev
But for some reason it doesn't work. It does work if I go to 127.0.0.1:8080, but not when I access http://sftablet.dev/. Also cleared the DNS cache.
答案1
得分: 3
http://sftablet.dev/
默认会查询端口80。
您的服务器只监听端口8080。http://sftablet.dev:8080/
应该可以正常工作。
英文:
http://sftablet.dev/
would by default query the port 80
Your server only listen to port 8080. http://sftablet.dev:8080/
should work.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论