node.js - How to add Reagent to Node JS API -
i have simple node js app serves api. can node main.js
server running , call localhost:3000/api/names
list of names. want build on reagent/cljs.
i can reagent app running on localhost:3030
. problem is, how make api calls node app reagent app? or architecture wrong? should combine 2 apps, , if so, how?
i have tried combining them, reagent wants run on ring, while node app wants run on node server. still have no communication between two. i've tried going through the quick start guide, isn't quite same situation. i've given this approach go no avail. easiest way put these pieces can bring api response data node app reagent app? or there way call make api calls within reagent app i'm missing?
yes, need communicate between code running in browser , code running on server.
the basic approach use xhr. client should this:
(ns foo (:require [goog.net.xhrio :as xhr])) (xhr/send "/api/names" (fn [e] (prn (.. e -target getresponsetext))))
alternatively there used library cljs-http
(ns foo (:require [cljs.core.async :refer [<!]] [cljs-http.client :as http]) (:require-macros [cljs.core.async.macros :refer [go]])) (go (let [response (<! (http/get "data.edn"))] (prn (:status response)) (prn (:body response))))
it uses core.async return results on channel. don't need care use though, except note things in go
blocks going happen 'later'.
for advanced usage, can create websockets sente
one important consideration web pages can perform xhr same host on same port served page. if hosting api @ localhost:3030, page must served localhost:3030 able communicate it. (this called same origin policy).
you stated in question have api on port 3000 , reagent app served 3030. not work because of same origin policy. there standard called cors cross origin resource sharing technically can use, in practice don't this. instead serve html/javascript same server api.
what means need sure when build reagent app, html page includes final javascript needs served same server serves api. matter of putting html , javascript resources folder on server.
Comments
Post a Comment