我想寫的HTML頁面時一定路徑被擊中:如何將HTML寫入句柄?
import Control.Monad
import Data.Char
import System.IO
import Network
import Data.Time.LocalTime
data RequestType = GET | POST deriving (Show)
data Request = Request { rtype :: RequestType, path :: String, options :: [(String,String)] }
data Response = Response { version :: String, statuscode :: Int }
instance Show Response where
show r = version(r) ++ " " ++ show(statuscode(r)) ++ " " ++ (case statuscode(r) of
100 -> "Continue"
200 -> "OK"
404 -> "Not Found") ++ "\r\n\r\n"
-- respond function
respond :: Request -> Handle -> IO()
respond request handle = do
putStrLn $ show request
let response = Response {version = "HTTP/1.1", statuscode = 200}
hPutStr handle $ show(response)
hPutStr handle $ "Haskell says " ++ (getMessage request)
where getMessage r
| (path r) == "/hello" = "<b>hello there!</b>" <-- HERE
| otherwise = (path r)
我可以運行沒有錯誤的代碼,但是當我訪問http:// {}主機名/你好我得到的字符串<b>hello there!</b>
表示html正在呈現爲一個字符串。
如何將其呈現爲html?
注意
我要做到這一點使用香草哈斯克爾,這意味着沒有第三方庫。
您使用的是什麼web框架/庫? – ErikR
請閱讀註釋,沒有框架 – dopatraman
您是否正在發佈Content-type:頭文件?什麼'hPutStr處理$顯示(響應)'實際打印出來? – ErikR