登录
首页 >  Golang >  Go问答

找不到文件的原因在这个简单的Web应用中是什么?

来源:stackoverflow

时间:2024-02-06 09:51:23 277浏览 收藏

本篇文章向大家介绍《找不到文件的原因在这个简单的Web应用中是什么?》,主要包括,具有一定的参考价值,需要的朋友可以参考一下。

问题内容

我正在开发 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文件:




    multiplication result
    


    

multiplication result

product: {{.product}}

以下是 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

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《找不到文件的原因在这个简单的Web应用中是什么?》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>