Suave没有显示静态文件

时间:2018-03-20 02:26:04

标签: f# suave

所以我的服务器设置非常简单。如果路径的格式为/article/something,则应提供文件夹something.html中的静态文件static。出于某种原因,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肯定存在于静态文件夹中,因此不存在问题。

关于可能导致此问题的任何想法?

1 个答案:

答案 0 :(得分:0)

以下是解决静态文件投放问题的部分内容

    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