dongqiuqiu4736 2017-10-13 11:50
浏览 12
已采纳

fasthttp:多个目录服务不起作用

Is there any way to serve multiple directory using fasthttp framework? I wrote the below code for the same purpose. But, this code is not working as I expected. When I access localhost:8080/path1, It throws error and warnings,

Cannot open requested path

2017/10/13 16:57:01 0.977 #0000000100000001 - 127.0.0.1:8080<->127.0.0.1:48870 - GET http://localhost:8080/path1 - cannot open file "/home/test/path1": open /home/test/path1/path1: no such file or directory

I don't know how this url(/home/test/path1) redirects to (/home/test/path1/path1). What is wrong with the below code ?

requestHandler := func(ctx *fasthttp.RequestCtx) {
        var fs fasthttp.FS
        switch string(ctx.Path()) {
        case "/path1":
            fs = fasthttp.FS{
                Root:       "/home/test/path1",
                IndexNames: []string{"index.html"},
            }
        case "/path2":
            fs = fasthttp.FS{
                Root:       "/home/test/path2",
                IndexNames: []string{"index.html"},
            }
        }
        fsHandler := fs.NewRequestHandler()
        fsHandler(ctx)
    }

    if err := fasthttp.ListenAndServe(":8080", requestHandler); err != nil {
        fmt.Println("error in ListenAndServe: %s", err)
    }
  • 写回答

1条回答 默认 最新

  • doufu7835 2017-10-13 11:55
    关注

    Nothing is wrong, works exactly as you wrote it: Root of webserver: /home/test/path1. You request http://bla/path1. This in turn translates into: http://bla/ -> /home/path/path1/index.html. http://bla/path1 -> /home/path/path1/path1/index.html.

    Ow yeah, in case of serving 2 directories - yes you can, just as any other normal HTTP server would, they just need to have the same Root. Otherwise look into virtual hosts support.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题