title: How can I communicate with the backend using ClojureScript and Figwheel?
body: <p>Note: I'm a moderately experienced programmer in general and using clojure but have never done serious web development.</p>
  
  <p>I set up a basic ClojureScript project using Chestnut and walked through the "Hello World" steps just fine. However I would really like to talk to my backend as well. For this purpose I redefined the Reagent code to be </p>
  
  <pre><code>(defn greeting []
    [:input {:type "button"
         :value (:text @app-state)
         :on-click #(http/get {})}])
  </code></pre>
  
  <p>Which gets a 404 response when clicked. So at least I'm talking to somebody. I can also see evidence of my get-requests in the server.log file. However at this point I'm struggling with a number of conceptual points.</p>
  
  <p>First of all http/get is a function defined in clj-http.client, which wasn't part of the Chestnut setup. 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.</p>
  
  <p>Secondly the file for the user namespace has the following lines predefined by Chestnut:</p>
  
  <pre><code>(def http-handler
    (wrap-reload #'mypage.server/http-handler))
  
  (defn run []
    (figwheel/start-figwheel!))
  </code></pre>
  
  <p>I can't see any place where the http-handler is ever used. So I don't understand what that definition even does.</p>
  
  <p>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. 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.</p>
  
  <p>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. 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. I have no reason to believe that Figwheel knows anything about the http-handlers I've defined in the server file.</p>
  
  <pre><code>(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
    (-&gt; routes
        (wrap-defaults api-defaults)
        wrap-with-logger
        wrap-gzip))
  </code></pre>