如何在Clojure中使用Ring运行Jetty示例
我将使用ring和jetty在Clojure中创建一个简单的web服务 我的项目中有这个。clj:如何在Clojure中使用Ring运行Jetty示例,clojure,jetty,leiningen,ring,Clojure,Jetty,Leiningen,Ring,我将使用ring和jetty在Clojure中创建一个简单的web服务 我的项目中有这个。clj: (defproject ws-example "0.0.1" :description "REST datastore interface." :dependencies [[org.clojure/clojure "1.5.1"] [ring/ring-jetty-adapter "0.2.5"] [ring-json-params "0.1.0"]
(defproject ws-example "0.0.1"
:description "REST datastore interface."
:dependencies
[[org.clojure/clojure "1.5.1"]
[ring/ring-jetty-adapter "0.2.5"]
[ring-json-params "0.1.0"]
[compojure "0.4.0"]
[clj-json "0.5.3"]]
:dev-dependencies
[[lein-run "1.0.0-SNAPSHOT"]])
此脚本位于script/run.clj中
(use 'ring.adapter.jetty)
(require '[ws-example.web :as web])
(run-jetty #'web/app {:port 8080})
在src/ws_example/web.clj中
(ns ws-example.web
(:use compojure.core)
(:use ring.middleware.json-params)
(:require [clj-json.core :as json]))
(defn json-response [data & [status]]
{:status (or status 200)
:headers {"Content-Type" "application/json"}
:body (json/generate-string data)})
(defroutes handler
(GET "/" []
(json-response {"hello" "world"}))
(PUT "/" [name]
(json-response {"hello" name})))
(def app
(-> handler
wrap-json-params))
但是,当我执行时:
lein run script/run.clj
我得到这个错误:
No :main namespace specified in project.clj.
为什么我会得到这个以及如何修复它?你必须把
(运行jetty)
东西放到-main
的某个地方,然后将它添加到项目中。clj
如
:main ws-example.core)
从“帮助运行”中的
:
USAGE: lein run -m NAMESPACE[/MAIN_FUNCTION] [ARGS...]
Calls the main function in the specified namespace.
因此,您需要将script.clj
放在项目源路径的某个位置,然后将其称为:
lein run -m script
出现此错误是因为lein run
(根据lein help run
)的目的是“运行项目的-main函数”。在ws示例.web
命名空间中没有-main
函数,在项目.clj
文件中也没有指定的:main
,这就是lein run所抱怨的
要解决这个问题,您有几个选项。您可以将run jetty
代码移动到ws example.web
函数的新-main
函数,然后说lein run-m ws example.web
。或者您可以这样做,还可以添加一行:main ws example.web
到project.clj
,然后只需说lein run
。或者您可以尝试使用来执行文件,而不是命名空间
有关更多信息,请查看。谢谢,您对这里的somehwhere有什么建议吗?run jetty的内容位于一个名为run.clj的脚本中。您链接到的教程使用Leiningen 1.x-您可能应该使用lein2。如果我能找到一个从我开始学习就可以使用的教程,那就太好了。有什么建议吗?我想在ClojureThis中使用lein2创建一个web服务。运行lein的命令在1.x中略有不同。