我正在尝试用GO语言制作简单的服务器,为什么它不加载页面而是继续显示错误消息?

发布于 2025-02-13 19:57:52 字数 1421 浏览 0 评论 0原文

我正在遵循一个教程,并且我已经对所有内容进行了交叉检查,似乎并没有工作.............................................................................................. .....................................................................


import (
   "fmt"
   "log"
   "net/http"
)

func formHandler(w http.ResponseWriter, r *http.Request) {
   if err := r.ParseForm(); err != nil {
       fmt.Fprintf(w, "ParseForm() err: %v", err)
       return
   }
   fmt.Fprintf(w, "POST request succesfull")
   name := r.FormValue("name")
   address := r.FormValue("address")
   fmt.Fprintf(w, "Name = %s\n", name)
   fmt.Fprintf(w, "Address = %s\n", address)

}
func helloHandler(w http.ResponseWriter, r *http.Request) {
   if r.URL.Path != "/hello" {
       http.Error(w, "404 not found", http.StatusNotFound)
       return
   }
   if r.Method != "Get" {
       http.Error(w, "method is not supported", http.StatusNotFound)
       return
   }
   fmt.Fprintf(w, "hello!")
}
func main() {
   fileServer := http.FileServer(http.Dir("./static"))
   http.Handle("/", fileServer)
   http.HandleFunc("/form", formHandler)
   http.HandleFunc("/hello", helloHandler)

   fmt.Printf("Starting server at port 8080\n")

   if err := http.ListenAndServe(":8080", nil); err != nil {
       log.Fatal(err)
   }
}

这是所有页面的错误消息

I am following a tutorial and I have literally crosschecked everything, It seems not to be working still ....................................................................


import (
   "fmt"
   "log"
   "net/http"
)

func formHandler(w http.ResponseWriter, r *http.Request) {
   if err := r.ParseForm(); err != nil {
       fmt.Fprintf(w, "ParseForm() err: %v", err)
       return
   }
   fmt.Fprintf(w, "POST request succesfull")
   name := r.FormValue("name")
   address := r.FormValue("address")
   fmt.Fprintf(w, "Name = %s\n", name)
   fmt.Fprintf(w, "Address = %s\n", address)

}
func helloHandler(w http.ResponseWriter, r *http.Request) {
   if r.URL.Path != "/hello" {
       http.Error(w, "404 not found", http.StatusNotFound)
       return
   }
   if r.Method != "Get" {
       http.Error(w, "method is not supported", http.StatusNotFound)
       return
   }
   fmt.Fprintf(w, "hello!")
}
func main() {
   fileServer := http.FileServer(http.Dir("./static"))
   http.Handle("/", fileServer)
   http.HandleFunc("/form", formHandler)
   http.HandleFunc("/hello", helloHandler)

   fmt.Printf("Starting server at port 8080\n")

   if err := http.ListenAndServe(":8080", nil); err != nil {
       log.Fatal(err)
   }
}

Here is the Error message for all pages

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘年祭陌 2025-02-20 19:57:53

我在您的代码中只看到一个问题,但它在/Hello处理程序中。您正在检查是否使用r.Method!=“ get”,但是http方法在大写中,因此您应该使用r.method!=“ get”甚至更好,使用给定常数r.method!= http.methodget。这为我解决了/Hello问题:

$ curl localhost:8080/hello
hello!

现在,/form处理问题不在您的代码中,但碰巧您正在尝试加载form.html而不是调用form,如果您在项目中的static> static subfolder中有一个名为form.html的文件,则可以工作。这样(这是一个非常极简主义示例):

<!DOCTYPE html>
<html>
  <title>Form</title>
  <body>

    <form method="post" action="/form">
      <label for="name">Name: </label>
      <input type="text" id="name" name="name" />
      <label for="address">Address: </label>
      <input type="text" id="address" name="address" />
      <input type="submit" />
    </form>
  </body>
</html>

这是因为您已经在http.handle(“/”,Fileserver)中处理静态文件。我不知道您正在关注的教程,但似乎是意图。

直接尝试表单的另一个选项,没有HTML可能会使用curl

$ curl -d 'name=My+Name&address=My+Address' localhost:8080/form
POST request succesfullName = My Name
Address = My Address

还有其他工具。 HTML应该可以训练。

I see only one issue in your code, but it is in the /hello handler. You are checking if it is a GET using r.Method != "Get", but HTTP methods are in uppercase, so you should use r.Method != "GET", or even better, use the given constant r.Method != http.MethodGet. That solves the /hello issue for me:

$ curl localhost:8080/hello
hello!

Now, the /form handle issue is not in your code, but happens that you are trying to load form.html instead of calling form, which can work if you have a file called form.html in the static subfolder in your project, like this (which is a very minimalist example):

<!DOCTYPE html>
<html>
  <title>Form</title>
  <body>

    <form method="post" action="/form">
      <label for="name">Name: </label>
      <input type="text" id="name" name="name" />
      <label for="address">Address: </label>
      <input type="text" id="address" name="address" />
      <input type="submit" />
    </form>
  </body>
</html>

That works because you are already handling static files in http.Handle("/", fileServer). I don't know the tutorial you are following, but looks like that was the intention.

Another option to try form directly, without HTML could be using something like curl:

$ curl -d 'name=My+Name&address=My+Address' localhost:8080/form
POST request succesfullName = My Name
Address = My Address

There are other tools. The HTML one should be fine for training.

昔日梦未散 2025-02-20 19:57:53

我可以看到您的HelloHandler函数在比较请求方法时,您应该将其与“ get”而不是“ get”进行比较。

有关更多信息,请查看: https://pkg.go.go.dev/net/net/http# pkg-contants

我在本地运行了此代码,它在本地为我运行良好。我上面提到的这种方法更改。您可以发布确切的错误吗?

I can see there is one more error in your helloHandler function when you compare the request Method, you should compare it with "GET" and not "Get".

For more info look at: https://pkg.go.dev/net/http#pkg-constants

I ran this code locally it is working fine locally for me. With this Method change I mentioned above. Can you post the exact error you are getting?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文