使用Echo服务器提供文件服务

huangapple go评论100阅读模式
英文:

Serving files with Echo

问题

我正在尝试使用Echo来提供多个文件,但并不总是有效。API代码如下:

package main

import (
	"github.com/labstack/echo"
	"net/http"
)

func main() {
	e := echo.New()

	e.GET("/", home)

    e.File("/data1", "assets/data1.csv")

    e.File("/data2", "assets/data2.csv")

	e.Logger.Fatal(e.Start(":4243"))
}

func home(c echo.Context) error {
  return c.String(http.StatusOK, "Are you lost?")
}

确切地说,它可以正常工作来获取第一个文件,但对于任何后续的调用(无论是文件获取还是更多的“经典”调用),它都会失败。每个浏览器的错误消息略有不同:

在Chrome中:

SyntaxError: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': 'Bearer {第一个获取的文件的全部内容}' 不是有效的HTTP头字段值。

在Firefox中:

SyntaxError: 指定了无效或非法的字符串。

在Edge中,简单地:

SyntaxError

尝试激活CORS,但没有任何变化。

在Postman中似乎工作得很好。也许这是我在应用程序中获取数据的方式有问题?

如果你需要更多信息,这个帖子与我之前的帖子直接相关(https://stackoverflow.com/questions/45543099/vuejs-with-axios-request-in-vuex-store-cant-make-more-than-one-request-why),但我不想混淆它们,因为我还不知道是我的Vue代码还是我的Echo代码有问题...

英文:

I'm trying to serve multiple files with Echo, but it doesn't work every time.
The api code looks like this:

package main

import (
	"github.com/labstack/echo"

	"net/http"
)

func main() {
	e := echo.New();

	e.GET("/", home);

    e.File("/data1", "assets/data1.csv");

    e.File("/data2", "assets/data2.csv");

	e.Logger.Fatal(e.Start(":4243"));
}

func home(c echo.Context) error {
  return c.String(http.StatusOK, "Are you lost?");
}

To be precise, it does work for the very first file fetching, but then keeps failing for any subsequent calls (be them file fetching or more "classic" calls). The error message is a tad different for each browser:

In Chrome:
>SyntaxError: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': 'Bearer {the_entire_content_of_the_first_fetched_file}' is not a valid HTTP header field value.

In Firefox:
>SyntaxError: An invalid or illegal string was specified

In Edge, simply:
>SyntaxError

Tried activating CORS, nothing changed.

Looks to work pretty well with Postman. Maybe it's a problem with how I do fetch my data in my application?

If you need perhaps a bit more information, this thread is directly related to my previous one (https://stackoverflow.com/questions/45543099/vuejs-with-axios-request-in-vuex-store-cant-make-more-than-one-request-why), but I didn't want to mix them up, as I don't know yet if I'm mistaken in my Vue code or my Echo one...

答案1

得分: 4

如果您想从特定目录传递文件,可以按照以下方式操作:

e.Use(middleware.Static("/path/to/directory"))

或者

fs := http.FileServer(http.Dir("/path/to/directory"))
e.GET("/assets/*", echo.WrapHandler(http.StripPrefix("/assets/", fs)))
英文:

If you would like to delivery files from certain directory. You can do following ways:

e.Use(middleware.Static("/path/to/directory"))

OR

fs := http.FileServer(http.Dir("/path/to/directory"))
e.GET("/assets/*", echo.WrapHandler(http.StripPrefix("/assets/", fs)))

答案2

得分: 0

只需添加默认的CORS中间件,我刚刚测试过,即使我使用不同的域名,它也能正常工作。
从echo框架导入这个包:"github.com/labstack/echo/middleware"
在你的路由之前添加以下默认的CORS配置:

e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
    AllowOrigins: []string{"*"},
    AllowMethods: []string{echo.GET, echo.PUT, echo.POST, echo.DELETE},
}))
英文:

Just Add default CORS Middleware, i just test and it's work fine even i using difference domain.
import this package from echo framework: "github.com/labstack/echo/middleware"
and Add this default CORS config befor your route:

e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
	AllowOrigins: []string{"*"},
	AllowMethods: []string{echo.GET, echo.PUT, echo.POST, echo.DELETE},
}))

答案3

得分: -1

嗯,看来一直以来都是我的应用程序的问题!我的 Echo API 看起来还好... 目前为止!

如果有人遇到相同的问题,也许这个链接可以帮到他们:https://stackoverflow.com/questions/45543099/vuejs-with-axios-request-in-vuex-store-cant-make-more-than-one-request-why/#45561091。

英文:

Welp, seems it was my application's fault all along! My Echo api seems fine... for now!

If someone has the same problem, perhaps this https://stackoverflow.com/questions/45543099/vuejs-with-axios-request-in-vuex-store-cant-make-more-than-one-request-why/#45561091 will help.

huangapple
  • 本文由 发表于 2017年8月8日 11:23:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/45558773.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定