简体   繁体   English

为什么我的fileServer处理程序不起作用?

[英]Why my fileServer handler doesn't work?

I've a simple folder : 我有一个简单的文件夹:

Test/
    main.go
    Images/
          image1.png
          image2.png
          index.html

In main main.go I just put : 在main main.go我刚才说:

package main

import (
       "net/http"
)

func main(){
     fs := http.FileServer(http.Dir("./Images"))
     http.Handle("/Images/*", fs)
     http.ListenAndServe(":3003", nil)
}

But when I curl on http://localhost:3003/Images/ or even I add to path file's name, it doesn't work. 但是,当我卷曲http:// localhost:3003 / Images /甚至我添加到路径文件的名称时,它不起作用。 I don't understand because it's the same as the reply given on this subject 我不明白,因为它与关于这个主题的答复是一样的

Can you tell me so that this does not work ? 你能告诉我这样做不行吗?

You need to remove * and add extra sub-folder Images : 您需要删除*并添加额外的子文件夹Images
This works fine: 这很好用:

Test/
    main.go
    Images/
          Images/
                image1.png
                image2.png
                index.html

Code: 码:

package main

import (
    "net/http"
)

func main() {
    fs := http.FileServer(http.Dir("./Images"))
    http.Handle("/Images/", fs)
    http.ListenAndServe(":3003", nil)
}

Then go run main.go 然后go run main.go

And: 和:

http://localhost:3003/Images/ HTTP://本地主机:3003 /图片/


Or simply use: 或者只是使用:

package main

import (
    "net/http"
)

func main() {
    fs := http.FileServer(http.Dir("./Images"))
    http.Handle("/", fs)
    http.ListenAndServe(":3003", nil)
}

with: http://localhost:3003/ 用: http:// localhost:3003 /

The reason the request failed to return what you expected is because they did not match the pattern defined in the http.Handle(pattern string, handler Handler) call. 请求未能返回预期的原因是因为它们与http.Handle(pattern string, handler Handler)调用中定义的模式不匹配。 The ServeMux documentation provides a description of how to compose patterns. ServeMux文档提供了如何组合模式的说明。 Any request is prefixed matched from most specific to least specific. 任何请求都是从最具体到最不具体的前缀匹配的。 It appears as though you have assumed a glob pattern can be used. 看起来好像假设可以使用glob模式。 Your handler would have been invoked with requests to /Images/*<file system path> . 您的处理程序将通过对/Images/*<file system path>请求进行调用。 You need to define a directory path like so, Images/ . 您需要定义像这样的目录路径Images/

On a side note, it is worth considering how your program gets the directory path to serve files from. 另外,值得考虑一下您的程序如何从中获取文件的目录路径。 Hard coding a relative means your program will only function within a specific location within the filesystem which incredibly brittle. 硬编码相对意味着您的程序只能在文件系统中的特定位置运行,这非常脆弱。 You could use a command line argument to allow users to specify a path or use a configuration file parsed at runtime. 您可以使用命令行参数来允许用户指定路径或使用在运行时解析的配置文件。 These considerations make your program easy to modularize and test. 这些考虑因素使您的程序易于模块化和测试。

Dot in ./Images refer cwd current working directory, not you project root. 点./Images指的是cwd当前的工作目录,而不是你的项目根目录。 For your server to work you must run it from Test/ directory, or address Images with absolute rooted path. 要使服务器正常工作,必须从Test /目录运行它,或者使用绝对根路径寻址映像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM