【问题标题】:Getting the http request when using compojure.route/not-found in Clojure在 Clojure 中使用 compojure.route/not-found 时获取 http 请求
【发布时间】:2023-03-24 07:26:01
【问题描述】:

我正在使用 Cloujre 的 Compojure 构建服务器。默认路由是compojure.route/not-found,有没有办法获取到达该路由的请求?我想打印所有最终到达那里的请求。

【问题讨论】:

    标签: clojure compojure


    【解决方案1】:

    您可以使用这种方法:

    (def handler (-> your-routes
                     wrap-my-request-middleware ;; it has to be in this order
                     ...))
    

    让我们在这里登录 uri

    (defn wrap-my-request-middleware
      [handler]
      (fn [request]
        (let [response   (handler request)]
          (when (= 404 (:status response))
            ;; do whatever you like in here
            (log/info (str "Request path: " (:uri request))))
          response)));; fn needs to return reponse...
    

    【讨论】:

    • 不行,还会让整个服务器发500错误
    • @shakedzy 这种方法看起来合法;您的 500 错误的内容是什么?
    • 我得到这个:HTTP 错误:500 访问 / 时出现问题。原因:响应映射为 nil 由 Jetty:// 提供支持 - 确实发生了日志记录,但仍然......
    • 我已经编辑了答案,所以代码可以工作 - 这个处理程序必须是最后一个(因为它调用它之前的处理程序)并且它必须返回 response,这就是导致500期
    • 这是为了演示,无论如何您都需要返回响应
    猜你喜欢
    • 1970-01-01
    • 2011-04-16
    • 1970-01-01
    • 2019-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    相关资源
    最近更新 更多