英文:
Custom 404 error message with Go
问题
我有这样一段代码,如果我访问/time/加上任何内容,它将显示自定义的404错误消息,但如果我访问/times/、/或/whatever,它将显示默认的404错误消息。我想在除了/time/之外的所有情况下都显示自定义的404页面。
package main
import (
"fmt"
"time"
"flag"
"os"
"net/http"
)
const AppVersion = "timeserver version: 3.0"
func timeserver(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/time/" {
NotFoundHandler(w, r)
return
}
const layout = "3:04:05 PM"
t := time.Now().Local()
fmt.Fprint(w, "<html>\n")
fmt.Fprint(w, "<head>\n")
fmt.Fprint(w, "<style>\n")
fmt.Fprint(w, "p {font-size: xx-large}\n")
fmt.Fprint(w, "span.time {color: red}\n")
fmt.Fprint(w, "</style>\n")
fmt.Fprint(w, "</head>\n")
fmt.Fprint(w, "<body>\n")
//fmt.Fprint(w, "The time is now " + t.Format(layout))
fmt.Fprint(w, "<p>The time is now <span class=\"time\">"+ t.Format(layout) +"</span>.</p>\n")
fmt.Fprint(w, "</body>\n")
fmt.Fprint(w, "</html>\n")
}
func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "custom 404")
}
func main() {
version := flag.Bool("V", false, "prints current version")
port := flag.String("port", "8080", "sets service port number")
flag.Parse()
if *version {
fmt.Println(AppVersion)
os.Exit(0)
}
http.HandleFunc("/time/", timeserver)
http.ListenAndServe("localhost:"+ *port, nil)
}
请问有什么我可以帮助你的吗?
英文:
I have this code that will give my custom 404 error message if I go to /time/<anything here will give custom 404> but if I go to /times/ or just / or /whatever then I will get the default 404 error message. I want to show my custom 404 for all cases other than /time/
package main
import (
"fmt"
"time"
"flag"
"os"
"net/http"
)
const AppVersion = "timeserver version: 3.0"
func timeserver(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/time/" {
NotFoundHandler(w, r)
return
}
const layout = "3:04:05 PM"
t := time.Now().Local()
fmt.Fprint(w, "<html>\n")
fmt.Fprint(w, "<head>\n")
fmt.Fprint(w, "<style>\n")
fmt.Fprint(w, "p {font-size: xx-large}\n")
fmt.Fprint(w, "span.time {color: red}\n")
fmt.Fprint(w, "</style>\n")
fmt.Fprint(w, "</head>\n")
fmt.Fprint(w, "<body>\n")
//fmt.Fprint(w, "The time is now " + t.Format(layout))
fmt.Fprint(w, "<p>The time is now <span class=\"time\">" + t.Format(layout) + "</span>.</p>\n")
fmt.Fprint(w, "</body>\n")
fmt.Fprint(w, "</html>\n")
}
func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "custom 404")
}
func main() {
version := flag.Bool("V", false, "prints current version")
port := flag.String("port", "8080", "sets service port number")
flag.Parse()
if *version {
fmt.Println(AppVersion)
os.Exit(0)
}
http.HandleFunc("/time/", timeserver)
http.ListenAndServe("localhost:" + *port, nil)
}
答案1
得分: 7
将以下内容添加到主函数中:
http.HandleFunc("/", NotFoundHandler)
"/" 的处理程序是通用处理程序。
此外,你应该修改处理程序以返回 404 状态:
func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, "custom 404")
}
英文:
Add this line to main:
http.HandleFunc("/", NotFoundHandler)
The handler for "/" is the catchall handler.
Also, you should modify the handler to return a 404 status:
func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, "custom 404")
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论