haskell - WAI 教程 - 没有实例(显示响应)
问题描述
同样是这里的 n00b:使用文档中的以下代码尝试 Warp 和 WAI。
{-# LANGUAGE OverloadedStrings, DeriveGeneric #-}
import Network.Wai
import Network.HTTP.Types
import Network.Wai.Handler.Warp (run)
app3 :: Application
app3 request respond = respond $ case rawPathInfo request of
"/" -> index
"/raw/" -> plainIndex
_ -> notFound
plainIndex :: Response
plainIndex = responseFile
status200
[("Content-Type", "text/plain")]
"index.html"
Nothing
notFound :: Response
notFound = responseLBS
status404
[("Content-Type", "text/plain")]
"404 - Not Found"
在 GHCi 中运行 plainIndex 会返回:
<interactive>:12:1: error:
• No instance for (Show Response) arising from a use of ‘print’
• In a stmt of an interactive GHCi command: print it
*Main> :r
[1 of 1] Compiling Main ( WAI.hs, interpreted )
两个问题合二为一:你能帮我解决这个问题吗,并且对此进行扩展:我是唯一一个在遵循文档示例时经常遇到此类问题的人?
解决方案
在 GHCi 中运行plainIndex
,GHCi 尝试计算Response
并在终端中打印。实例定义给Show
定类型应如何表示为String
. 库作者选择不为 提供Show
实例Response
,可能是为了将其表示与接口分开。
响应的各个部分都有Show
实例,因此您可以使用Wai 提供的访问器:
> responseStatus plainIndex
> responseHeaders plainIndex
推荐阅读
- css - 从 Angular 应用程序复制/粘贴时保持文本格式
- reactjs - react-leaflet 中的 GeoJSON 不会在地图上渲染图层
- java - 如何在经典(非引导)Spring 应用程序中注册 MessageDispatcherServlet
- vb.net - PostgreSQL:VB.NET ODBC 更新无法正常工作
- regex - 正则表达式 - 找出包含一个单词或其子字符串后不跟某个字符的字符串
- swift - 如何在 macOS 中获取实时麦克风输入?
- bash - systemctl service systemd-notify 不适用于非 root 用户
- excel - 我想获取“另存为”菜单中列出的“当前文件夹”
- vue.js - 从 [__ob__: Observer] 中提取数据
- javascript - 我的列表项不会在单击按钮时删除