【发布时间】:2017-03-23 02:54:49
【问题描述】:
我正在学习 Clojure 环。这是我的第一个处理程序:
(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}})
当我跑步时: lein ring 无头服务器 并浏览至http://localhost:3000 我看到它在控制台上打印“你好,世界!”两次。为什么打印2次?我想它只打印一次。 然后我修改源代码为:
...{:body args :status 200}...
然后使用谷歌浏览器浏览到http://localhost:3000 这一次,它打印出“你好,世界!”在控制台上 3 次。为什么改为打印3次?
我正在使用 REPL-y 0.3.7、nREPL 0.2.12、Clojure 1.8.0、lein-ring "0.9.7"、Windows 10-64 位。
【问题讨论】:
-
我想发生的事情是您在更改代码后调用
-main,并在您的控制台上累积Hello, World!s。 -
不,绝对不。我没有计算累积的结果。每次浏览器刷新都会产生 2 个“Hello, world!”。
-
-main是您的处理程序吗?您应该使用其他函数作为处理程序。-main只能用作应用程序的入口点。 -
是的,它是处理程序。我刚刚将
-main重命名为other-functions。结果相同。 -
您能否编辑您的问题,并粘贴您文件中的所有代码。