为什么我需要使用 http.StripPrefix 来访问我的静态文件? [英] Why do I need to use http.StripPrefix to access my static files?

查看:30
本文介绍了为什么我需要使用 http.StripPrefix 来访问我的静态文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

main.go

package main

import (
    "net/http"
)

func main() {
    http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
    http.ListenAndServe(":8080", nil)
}

目录结构:

%GOPATH%/src/project_name/main.go
%GOPATH%/src/project_name/static/..files and folders ..

即使在阅读了文档之后,我也无法理解 http.StripPrefix 在这里到底做了什么.

Even after reading the documentation I have trouble understanding what exactly http.StripPrefix does here.

1) 如果删除 http.StripPrefix,为什么我无法访问 localhost:8080/static?

1) Why can't I access localhost:8080/static if I remove http.StripPrefix?

2) 如果删除那个函数,什么 URL 映射到 /static 文件夹?

2) What URL maps to /static folder if I remove that function?

推荐答案

http.StripPrefix() 将请求的处理转发给您指定为参数的请求,但在此之前它会通过去除指定的前缀来修改请求 URL.

http.StripPrefix() forwards the handling of the request to one you specify as its parameter, but before that it modifies the request URL by stripping off the specified prefix.

例如在您的情况下,如果浏览器(或 HTTP 客户端)请求资源:

So for example in your case if the browser (or an HTTP client) requests the resource:

/static/example.txt

StripPrefix 将剪切 /static/ 并将修改后的请求转发给 http.FileServer() 所以它会看到请求的资源是

StripPrefix will cut the /static/ and forward the modified request to the handler returned by http.FileServer() so it will see that the requested resource is

/example.txt

http.FileServer() 返回的 Handler 将查找和提供文件的内容相对到文件夹(或者更确切地说FileSystem) 指定为其参数(您指定 "static" 作为静态文件的根).

The Handler returned by http.FileServer() will look for and serve the content of a file relative to the folder (or rather FileSystem) specified as its parameter (you specified "static" to be the root of static files).

现在由于 "example.txt" 位于 static 文件夹中,您必须指定一个相对路径才能获得正确的文件路径.

Now since "example.txt" is in the static folder, you have to specify a relative path to that to get the corrent file path.

可以在 http 包文档页面(此处)上找到此示例:

This example can be found on the http package documentation page (here):

// To serve a directory on disk (/tmp) under an alternate URL
// path (/tmpfiles/), use StripPrefix to modify the request
// URL's path before the FileServer sees it:
http.Handle("/tmpfiles/",
        http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp"))))

说明:

FileServer() 告诉静态文件的根是"/tmp".我们希望 URL 以 "/tmpfiles/" 开头.所以如果有人请求"/tempfiles/example.txt",我们希望服务器发送文件"/tmp/example.txt".

FileServer() is told the root of static files is "/tmp". We want the URL to start with "/tmpfiles/". So if someone requests "/tempfiles/example.txt", we want the server to send the file "/tmp/example.txt".

为了实现这一点,我们必须从 URL 中去除 "/tmpfiles",剩下的将是相对于根文件夹 "/tmp" 的相对路径代码>,如果我们加入它会给出:

In order to achieve this, we have to strip "/tmpfiles" from the URL, and the remaining will be the relative path compared to the root folder "/tmp" which if we join gives:

/tmp/example.txt

这篇关于为什么我需要使用 http.StripPrefix 来访问我的静态文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆