首页 > 解决方案 > 如何在 Kemal after_all 方法中访问路由的返回值?

问题描述

从 Kemal 文件中的 after_all 处理程序,我如何修改来自路由的响应?[见下面的例子]

VERSION = "0.1.0"

require "kemal"

# Configure kemal parameters
serve_static false

get "/" do
  "Hello world!"
end

after_all do |env|
  # Would like to inject something here that turns "Hello world!" into "HELLO WORLD!", 
  # but I'm not sure how to get the original ("Hello world!") in this scope. 
end
Kemal.run

该文档没有任何 after_all 路由的示例,而且我似乎无法在包含它的上下文中找到任何对象。我该怎么做?

标签: crystal-langkemal

解决方案


你不能那样做。路由处理程序的返回值立即发送到底层套接字。基本上没有办法检索或更改它。

相反,您应该考虑直接在显式代码中实现您想要的。Kemal 处理程序不是为此而设计的。您应该只将它们用于与 HTTP 协议相关的任务。


推荐阅读