【问题标题】:Suave not showing static fileSuave 不显示静态文件
【发布时间】:2018-08-28 17:56:13
【问题描述】:

所以我的服务器设置非常简单。如果路径的格式为/article/something,它应该提供文件夹static 中的静态文件something.html。出于某种原因,Files.file webpart 显然返回了None。我添加了OK "File Displayed" webpart 来验证情况是否如此。 OK 永远不会执行。

let app =
    choose [
        pathScan "/article/%s" (fun article ->
                                  let name = sprintf "%s.html" article
                                  Console.WriteLine name
                                  Files.file name >=> OK "File Displayed")
    ]

let config =
    { defaultConfig with homeFolder = Some (Path.GetFullPath "./static") }

[<EntryPoint>]
let main args =
    startWebServer config app
    0

有趣的是,Console.WriteLine name 行完美执行,当我执行此命令时,我在控制台窗口中看到something.html。看来问题完全是 Files.file name 返回无。

文件something.html肯定存在于静态文件夹中,所以这不是问题。

有什么可能导致这种情况的想法吗?

【问题讨论】:

  • 你确认File.file name确实返回None了吗?
  • 我还会检查“./static”扩展的绝对路径,因为这可能是相对于进程的工作目录,这取决于谁启动它以及从哪里启动
  • @HonzaBrestan 谢谢!这并不是正在发生的事情,但它帮助我找到了问题所在。我使用的是 Windows,所以它不喜欢我说 "./static" 而不是 ".\\static"

标签: f# suave


【解决方案1】:

以下是解决静态文件服务问题的一些部分

    let troubleShootExtensionPart extensionToCheck :WebPart =
        fun ctx ->
            match extensionToCheck with
            | null | "" -> ServerErrors.INTERNAL_ERROR "Extension Error not supplied, part is not set up correctly"
            | x when not <| x.StartsWith "." -> ServerErrors.INTERNAL_ERROR "Extensions start with a '.', part is not set up correctly"
            | _ ->
                let mtm = ctx.runtime.mimeTypesMap
                match mtm extensionToCheck with
                | None ->
                    sprintf "%s is not supported by the mime types map, compose your mime type with the `defaultMimeTypesMap`" extensionToCheck
                    |> RequestErrors.FORBIDDEN
                | Some x ->
                    sprintf "%s is supported and uses '%s', compression on? : %A" extensionToCheck x.name x.compression
                    |> OK
            |> fun wp -> wp ctx

使用通配符的示例消费,因此如果没有匹配的路由,您将获得一些诊断信息

#if DEBUG
pathScan "/checkExtension/%s" (fun name -> troubleShootExtensionPart name)
// catch all
(fun ctx -> sprintf "404, also homeFolder resolves to %s" (Path.GetFullPath ".") |> RequestErrors.NOT_FOUND |> fun wp -> wp ctx)
#endif

【讨论】:

    猜你喜欢
    • 2017-05-12
    • 1970-01-01
    • 2020-01-04
    • 2021-05-30
    • 2013-10-26
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    • 2013-09-14
    相关资源
    最近更新 更多