这一定是一件非常愚蠢的事情。我刚刚开始玩 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/