首页 > 文章列表 > 为何在此简易网页应用中无法找到文件

为何在此简易网页应用中无法找到文件

457 2025-03-12
问题内容

我正在开发 debian stable linux,它运行得很好。我有一个简单的 web 应用程序的以下目录结构,该应用程序从用户处获取 2 个数字并返回这 2 个数字的乘积:

.
├── ./main
├── ./main.go
└── ./static
    ├── ./static/favicon.ico
    ├── ./static/index.html
    └── ./static/style.css

我有以下 go 代码:

package main
import (
    "fmt"
    "html/template"
    "log"
    "net/http"
    "strconv"
)
type viewdata struct {
    product int
}
func multiplyhandler(w http.responsewriter, r *http.request) {
    err := r.parseform()
    if err != nil {
        http.error(w, "error parsing form", http.statusbadrequest)
        return
    }
    num1, err := strconv.atoi(r.form.get("num1"))
    if err != nil {
        http.error(w, "invalid input for num1", http.statusbadrequest)
        return
    }
    num2, err := strconv.atoi(r.form.get("num2"))
    if err != nil {
        http.error(w, "invalid input for num2", http.statusbadrequest)
        return
    }
    product := num1 * num2
    data := viewdata{
        product: product,
    }
    tmpl, err := template.parsefiles("index.html")
    if err != nil {
        http.error(w, "error parsing template", http.statusinternalservererror)
        return
    }
    err = tmpl.execute(w, data)
    if err != nil {
        http.error(w, "error rendering template", http.statusinternalservererror)
        return
    }
}

func main() {
    http.handle("/static/", http.stripprefix("/static/", http.fileserver(http.dir("./static"))))
    http.handlefunc("/multiply", multiplyhandler)
    fmt.println("server listening on port 8080")
    log.fatal(http.listenandserve(":8080", nil))
}

以下是index.html文件:

<!doctype html>
<html>
<head>
    <title>multiplication result</title>
    <link rel="stylesheet" type="text/css" href="/static/style.css">
</head>
<body>
    <h1>multiplication result</h1>
    <p>product: {{.product}}</p>
</body>
</html>

以下是 style.css 文件:

body {
    font-family: arial, sans-serif;
    background-color: #f5f5f5;
    margin: 0;
    padding: 20px;
}
h1 {
    color: #333;
}
p {
    color: #666;
    font-size: 18px;
    margin-bottom: 10px;
}

go 源代码文件 main.go 构建时没有任何警告或错误消息。

然后我使用命令运行它:./main。以下是消息内容:

server listening on port 8080

然后我打开 firefox 浏览器并输入 url: http://localhost:8080 。然而,网页只显示:

404 page not found

问题出在哪里,如何解决?


正确答案


以下是对代码的更改(第二个更改不是必需的,但它简化了代码):

(注意:更改以 diff 格式显示。但不幸的是,这不是 stackoverflow 突出显示的语法)

package main
 
 import (
    "fmt"
    "html/template"
    "log"
    "net/http"
    "strconv"
 )
 
 type viewdata struct {
    product int
 }
 
 func multiplyhandler(w http.responsewriter, r *http.request) {
    err := r.parseform()
    if err != nil {
        http.error(w, "error parsing form", http.statusbadrequest)
        return
    }
    num1, err := strconv.atoi(r.form.get("num1"))
    if err != nil {
        http.error(w, "invalid input for num1", http.statusbadrequest)
        return
    }
    num2, err := strconv.atoi(r.form.get("num2"))
    if err != nil {
        http.error(w, "invalid input for num2", http.statusbadrequest)
        return
    }
    product := num1 * num2
    data := viewdata{
        product: product,
    }
-   tmpl, err := template.parsefiles("index.html")
+   tmpl, err := template.parsefiles("static/index.html")
    if err != nil {
        http.error(w, "error parsing template", http.statusinternalservererror)
        return
    }
    err = tmpl.execute(w, data)
    if err != nil {
        http.error(w, "error rendering template", http.statusinternalservererror)
        return
    }
 }
 
 func main() {
-   http.handle("/static/", http.stripprefix("/static/", http.fileserver(http.dir("./static"))))
+   http.handle("/static/", http.fileserver(http.dir(".")))
    http.handlefunc("/multiply", multiplyhandler)
    fmt.println("server listening on port 8080")
    log.fatal(http.listenandserve(":8080", nil))
 }

然后在浏览器中打开此网址(提供所需参数num1num2):

http://localhost:8080/multiply?num1=5&num2=10