dongyuqie4322 2015-02-26 14:35
浏览 109
已采纳

使用Go的网络服务器,网站的根目录在哪里映射到文件系统?

Where is the filesystem "root" of a Go net/http webserver. It doesn't seem to be in the directory the executable is in. By "root" I mean the directory I would use for, say, the src attribute of an img, without any path. I don't plan to do this, but it would help me understand the structure if I knew.

  • 写回答

1条回答 默认 最新

  • dou29106 2015-02-26 14:41
    关注

    Introduction

    In Go the net/http package is used to provide Web Server functionality. This is not a static fileserver, it is much more than that.

    There is no filesystem "root" concept. The provided web server uses handlers to serve HTTP requests which are mapped to URLs. A handler is responsible to process an HTTP request and to setup and generate the response. A handler can be registered e.g. with the Handle() or HandleFunc() functions. The server can be started with the ListenAndServe() function.

    Read the package documentation of net/http to understand the basic concepts and to get started. It also contains many small examples.

    The Blog article Writing Web Applications is also helpful.

    Static File Server

    However, a static file server or "filesystem" functionality is provided, there is a FileServer() function in the http package which returns a Handler which serves static files. You can specify the "root" folder to serve static files from as a parameter of FileServer().

    If you pass an absolute path to FileServer(), then there is no question what that means. If you provide a relative path, it is always interpreted in the context of the current or working directory. By default this is the folder you start the application from (the folder you're in when executing the go run ... command or the compiled executable binary).

    Example:

    http.Handle("/", http.FileServer(http.Dir("/tmp")))
    

    This will setup a handler to serve files from the folder /tmp mapped to the root URL /. For example the response to the GET request "/mydoc.txt" will be the "/tmp/mydoc.txt" static file.

    Complete application:

    package main
    import (
        "log"
        "net/http"
    )
    func main() {
        // Simple static webserver:
        http.Handle("/", http.FileServer(http.Dir("/tmp")))
        log.Fatal(http.ListenAndServe(":8080", nil))
    }
    

    You can do more complex mapping using the StripPrefix() function. Example:

    // 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"))))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line