我有以下Main.hs
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Web.Scotty
import Network.Wai.Middleware.RequestLogger
import Network.Wai.Middleware.Static
import Text.Blaze.Html.Renderer.Text (renderHtml)
import qualified Text.Blaze.Html5 as H
import Text.Blaze.Html5.Attributes
import Control.Monad.IO.Class
main :: IO ()
main = do
scotty 3000 $ do
middleware logStdoutDev
middleware $ staticPolicy (noDots >-> addBase "static")
get "/" $ do
html $ renderHtml $
H.docTypeHtml $
H.html $ do
H.head $
H.link H.! rel "stylesheet" H.! href "stylesheet.css"
H.body $ do
H.h1 "Link Shortener"
H.form H.! method "POST" H.! action "/shorten" $ do
H.input H.! type_ "text" H.! name "url"
H.input H.! type_ "submit" H.! value "Shorten!"
post "/shorten" $ do
url <- param "url"
liftIO $ putStrLn url
redirect "/"
和以下目錄結構(這是在一個stack專案中):

據我從閱讀中得知,一個 GET 請求/stylesheet.css應該為樣式表提供服務,但我得到一個 404。一個請求/static/stylesheet.css也是一個 404。我的中間件策略或目錄結構有問題嗎?
uj5u.com熱心網友回復:
這可能是一個“當前目錄”問題。從 Stack 專案目錄中運行時,可執行檔案通常應在其當前目錄設定為專案目錄的根目錄而不是app子目錄的情況下啟動。如果您將static目錄上移一級,那可能會修復它。
我能夠使您的代碼在具有stack new ... simple專案static根目錄的子檔案夾的專案中正常作業。
scottystatic/
|
| stack.yaml, scottystatic.cabal, etc.
|
-- src/
| Main.hs
|
`-- static/
stylesheet.css
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/447870.html
上一篇:GHC錯誤訊息逐字參考型別族定義
