简体   繁体   English

如何使用ClojureScript和Figwheel与后端进行通信?

[英]How can I communicate with the backend using ClojureScript and Figwheel?

Note: I'm a moderately experienced programmer in general and using clojure but have never done serious web development. 注意:我是一名经验丰富的程序员,使用clojure但从未进行过严肃的Web开发。

I set up a basic ClojureScript project using Chestnut and walked through the "Hello World" steps just fine. 我使用Chestnut设置了一个基本的ClojureScript项目,并且完成了“Hello World”步骤。 However I would really like to talk to my backend as well. 但是,我真的想和我的后端交谈。 For this purpose I redefined the Reagent code to be 为此,我重新定义了试剂代码

(defn greeting []
  [:input {:type "button"
       :value (:text @app-state)
       :on-click #(http/get {})}])

Which gets a 404 response when clicked. 单击时获得404响应。 So at least I'm talking to somebody. 所以至少我和某个人说话。 I can also see evidence of my get-requests in the server.log file. 我还可以在server.log文件中看到我的get-requests的证据。 However at this point I'm struggling with a number of conceptual points. 然而,在这一点上,我正在努力解决一些概念问题。

First of all http/get is a function defined in clj-http.client, which wasn't part of the Chestnut setup. 首先,http / get是clj-http.client中定义的函数,它不是Chestnut设置的一部分。 It feels like I'm already off track if I have to go hunting for libraries to send something as basic as a get-request. 如果我不得不去寻找图书馆发送基本内容作为获取请求,我觉得我已经偏离了轨道。

Secondly the file for the user namespace has the following lines predefined by Chestnut: 其次,用户命名空间的文件具有Chestnut预定义的以下行:

(def http-handler
  (wrap-reload #'mypage.server/http-handler))

(defn run []
  (figwheel/start-figwheel!))

I can't see any place where the http-handler is ever used. 我看不到任何使用http处理程序的地方。 So I don't understand what that definition even does. 所以我不明白这个定义甚至是什么。

Also the way I understand Figwheel, when I call "run" it'll spin up a new web server which then a) serves the index.html and b) connects to my browser via some TCP port and starts pumping new JavaScript through that connection. 另外我理解Figwheel的方式,当我调用“run”时它会启动一个新的web服务器,然后a)服务index.html和b)通过一些TCP端口连接到我的浏览器并开始通过该连接泵送新的JavaScript 。 This second part is very speculative on my part. 第二部分对我来说非常具有推测性。 If this is actually what happens my next question would be if Figwheel also needs to sit on the other side of that connection or if browsers have some common API that allows code reloading from the outside. 如果这实际发生了什么,我的下一个问题是,如果Figwheel还需要坐在该连接的另一端,或者浏览器是否有一些允许从外部重新加载代码的通用API。

Lastly I can kinda tell that the ring routes and http-handler defined in the mypage/server.clj file (below) are being called somehow, since modifying these changes the error from the get-request, however it's a complete mystery to me how this works. 最后我可以告诉我们以某种方式调用mypage / server.clj文件(下面)中定义的环路由和http-handler,因为修改这些会改变get-request中的错误,但是对我来说这是一个完全的谜。这很有效。 The way I understand it the get-request I'm sending from the browser is sent to the Figwheel-server, the origin of the site. 我理解它的方式是我从浏览器发送的get-request被发送到Figwheel-server,即站点的起源。 I have no reason to believe that Figwheel knows anything about the http-handlers I've defined in the server file. 我没有理由相信Figwheel知道我在服务器文件中定义的http处理程序。

(defroutes routes
  (GET "/" _
    {:status 200
     :headers {"Content-Type" "text/html; charset=utf-8"}
     :body (io/input-stream (io/resource "public/index.html"))})
  (resources "/"))

(def http-handler
  (-> routes
      (wrap-defaults api-defaults)
      wrap-with-logger
      wrap-gzip))

对于ClojureScript项目(即不是Clojure),我相信你想要这个: https//github.com/r0man/cljs-http

I don't have a complete answer, but I do have a couple points that might help. 我没有完整的答案,但我确实有几点可能有所帮助。

  1. Clojurescript ultimately compiles down to JavaScript, and makes heavy use of (and has access to) the Google Closure library. Clojurescript最终编译成JavaScript,并大量使用(并且可以访问)Google Closure库。 So if you really want to, you can use JavaScript interop to just make an AJAX call from the client-side, just like you would in JS. 因此,如果您真的想要,可以使用JavaScript互操作来从客户端进行AJAX调用,就像在JS中一样。 The closure library provides a wrapper for this---see docs here https://developers.google.com/closure/library/docs/xhrio . 封闭库为此提供了一个包装器 - 请参阅https://developers.google.com/closure/library/docs/xhrio中的文档。 But there are also several easy http and Ajax libraries for clojurescript, so why not use them? 但是clojurescript也有几个简单的http和Ajax库,为什么不使用它们? Another part of the joy and magic of clojurescript is that the Google closure optimizations that get applied do lovely things like strip out dead code, so I believe (and someone else can correct me if I'm wrong) that there is little (no?) production cost to putting some extra libraries in. clojurescript的喜悦和魔力的另一部分是,应用的谷歌闭包优化做了一些可爱的事情,比如删除死代码,所以我相信(如果我错了,其他人可以纠正我)那么(不是吗?)生产成本增加一些额外的库。

  2. Figwheel is ultimately dev, not production, and doesn't depend on the http server that you set up for production use. Figwheel最终是dev,而不是生产,并且不依赖于您为生产使用而设置的http服务器。 Indeed, there are templates out there for front-end-only cljs projects that still use figwheel--- here's one example . 事实上,有一些模板可用于仅使用figwheel的前端cljs项目---这是一个例子 Figwheel spins its own server to push changes to the browser, I'm not quite sure how it works. Figwheel旋转自己的服务器将更改推送到浏览器,我不太确定它是如何工作的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM