haskell - 网络斯科蒂 : file not found while serving static files

标签 haskell haskell-wai

这一定是一件非常愚蠢的事情。我刚刚开始玩 scotty,无法正确提供静态内容。

import Network.HTTP.Types
import Web.Scotty
import qualified Data.Text as T
import Data.Monoid (mconcat)
import Data.Aeson (object, (.=))
import Network.Wai.Middleware.Static

main = scotty 3000 $ do
  middleware $ staticPolicy (noDots >-> addBase "static")
  get "/" $ file "index.html"

非常简单。这就是您在几个 scotty 教程中找到的内容。但由于某种原因它不起作用。在浏览器中通过“/”访问会出现“找不到文件”的情况。 如果我在浏览器中输入“/index.html” - 它就可以工作。但这是错误的!我希望它可以通过“/”访问,但不能通过“/index.html”访问。它忽略根并直接获取 html 文件。如何通过“/”根目录提供index.html?周围没有太多信息,我发现一些教程指向上面的示例或类似的示例,但它们无法按预期工作。

我尝试将标题设置为“text/html”,但什么都没有......不走运。

最佳答案

If I type '/index.html' at the browser - it works. But it is wrong! I want it to be accessible via '/' but not '/index.html'.

但这正是 staticPolicy 的意思中间件是为了!每当请求与策略匹配时(在本例中文件位于 ./static/中),它将被 staticPolicy 过滤。如果您不希望出现这种行为,请删除中间件。

Accessing via '/' in the browser gives me the 'file not found' thing.

因为中间件操作没有为以下命令设置相对路径。您需要提供 file 的完整路径:

main = scotty 3000 $ do
  get "/" $ file "./static/index.html"

关于haskell - 网络斯科蒂 : file not found while serving static files,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22662826/

相关文章:

haskell - 我应该在 REPL Haddock 示例中使用 'let' 吗?

haskell - wai 的意外管道行为

javascript - Haskell WARP/WAI 服务器无法响应来自 Android 嵌入式 WebView 的 AJAX 调用

haskell - 如何限制 WAI 中请求正文和 header 的大小?

http - 带有 WAI 的斐波那契 Web 服务

haskell - Haskell 运行时统计中的负生产力

Haskell 秒差距问题

haskell - 不能从 f x = f₁ y 推导出 f = f₁?

haskell - 是否可以在 GADT 定义中使用类型别名?

haskell - 将数据放入请求库的中间件