clojure环中的重复评估

时间:2017-03-23 02:54:50

标签: clojure ring

我正在学习Clojure Ring。这是我的第一个处理程序:

(ns long-hdi.core
 (:gen-class))

(defn -main
  "I don't do a whole lot ... yet."
  [& args]
  (println "Hello, World!"){:body "hello" :status 200})

(defn on-init[](println "Initializing..."))
(defn on-destroy[](println "destroying..."))

这是project.clj配置文件:

   (defproject long-hdi "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.8.0"]]
  :plugins [[lein-ring "0.9.7"]]
  :ring {:handler long-hdi.core/-main :init long-hdi.core/on-init :destroy long-hdi.core/on-destroy}
  :main ^:skip-aot long-hdi.core
  :target-path "target/%s"
  :profiles {:uberjar {:aot :all}})

当我跑步时: 莱恩环服务器 - 无头 并浏览到http://localhost:3000 我看到它在控制台上打印“Hello,world!”两次。为什么要打印2次?我想它只打印一次。 然后我将源代码修改为:

...{:body args :status 200}...

然后使用谷歌浏览器浏览http://localhost:3000 这一次,打印出“Hello,world!”在控制台上3次。为什么它会改为打印3次?

我使用的是REPL-y 0.3.7,nREPL 0.2.12,Clojure 1.8.0,lein-ring“0.9.7”,Windows 10-64位。

1 个答案:

答案 0 :(得分:4)

由于您的代码问题没有发生,但我会在下面向您展示如何修改代码以获得您想要的结果。

要在此处进行更多调查,请打开Chrome开发工具窗口(右键单击并选择“检查”)。转到顶部的“网络”选项卡。在左侧,您会在请求列表下看到一些有趣的内容:localhost以及favicon.ico。因此,实际上,每次重新加载浏览器时,它不仅会对页面发出请求,还会向页面图标发出请求,这就是您看到两个请求的原因。 Firefox(至少在我的情况下)只是在第一次加载后缓存图标,但chrome每次都会从服务器请求它。

那么,你能做些什么呢?好吧,浏览器的每个请求都会调用相同的处理程序。所以,你需要接下来探索路线。这允许您将特定的浏览器请求映射到特定的处理程序。因此,您可以简单地避免调用favicon。作为一个让您入门的小工作示例,它允许您在浏览器窗口中查看请求,而不是打印到命令行:

您的project.clj:

(defproject long-hdi "0.1.0-SNAPSHOT"
  :dependencies [[org.clojure/clojure "1.8.0"]
                 [ring/ring-core "1.5.1"]
                 [ring/ring-jetty-adapter "1.5.1"]
                 [compojure "1.5.2"]]
  :main long-hdi.core)

在您的主要源文件中:

(ns long-hdi.core
  (:require [ring.adapter.jetty]
            [compojure.core :refer :all]))

(def counter (atom 0))

(defn handler
  [req]
  (swap! counter inc)
  {:body (str "counter is: " @counter) :status 200})

(defroutes app
  (GET "/" [] handler))

(defn -main [& args]
  (ring.adapter.jetty/run-jetty app {:port 3000}))

运行服务器,然后在localhost:3000

的浏览器窗口中将其打开
$ lein run

我希望这会有所帮助,并且玩得开心!

相关问题