grunt
/webpack
/express
,我可以將其他域/主機名的一些API代理到提供html頁面的當前服務器並解決CORS問題。如何在`figwheel` dev服務器中做代理?
我發現figwheel
使用ring
啓動http服務器,那麼我想我可以使用https://github.com/tailrecursion/ring-proxy到代理路徑添加到figwheel
服務器。但我不知道如何在figwheel
項目之外做到這一點。
謝謝!
grunt
/webpack
/express
,我可以將其他域/主機名的一些API代理到提供html頁面的當前服務器並解決CORS問題。如何在`figwheel` dev服務器中做代理?
我發現figwheel
使用ring
啓動http服務器,那麼我想我可以使用https://github.com/tailrecursion/ring-proxy到代理路徑添加到figwheel
服務器。但我不知道如何在figwheel
項目之外做到這一點。
謝謝!
我使用Luminus
框架,並已將此添加Luminus
框架的middleware.clj
:
(defn create-proxy [handler fns]
;; TODO check whether cookies are sent
(let [identifier-fn (get fns :identifier-fn identity)
host-fn (get fns :host-fn {})
path-fn (get fns :path-fn identity)]
(fn [request]
(let [request-key (identifier-fn request)
host (host-fn request-key)
stripped-headers (dissoc (:headers request) "content-length" "host")
path (path-fn (:uri request))]
(if host
(->
{:url (build-url host (path-fn (:uri request)) (:query-string request))
:method (:request-method request)
:body (if-let [len (get-in request [:headers "content-length"])]
(slurp-binary
(:body request)
(Integer/parseInt len)))
:headers stripped-headers
:follow-redirects true
:throw-exceptions false
:as :stream
:insecure? true
}
clj-http.client/request
(update-in [:headers] dissoc "Transfer-Encoding")
)
(handler request))
))))
(defn wrap-proxy [handler]
(-> handler
(create-proxy
{:identifier-fn :uri
:host-fn (proxy-map-2-host-fn proxy-map)
:path-fn (fn [^String uri]
(subs uri (inc (.indexOf (rest uri) \/))))})))
和handler.clj
改變app-routes
這樣:
(def app-routes
(routes
(-> #'home-routes
(wrap-routes middleware/wrap-csrf)
(wrap-routes middleware/wrap-formats))
(middleware/wrap-proxy
(route/not-found
(:body
(error-page {:status 404
:title "page not found"}))))))
的proxy-map
可能是這樣的:
(def proxy-map "proxy map"
{"/www" "https://www.example.com"
"/test" "http://test.example.com"
}
)
它的工作原理是
。但是,在使用此框架時,我不需要cider-jack-in-clojurescript
,並且每當我保存我的.cljs文件時都會重新加載網頁。因此,只需將任何跨站點路線添加到proxy-map
並使用相應的密鑰即可訪問。
例如,使用(GET "/www/some-path'")
將起作用,如果您要求(GET "https://www.example.com")
。
順便說一句,這些代碼中的大部分都從https://github.com/tailrecursion/ring-proxy和其他一些類似的回購中被盜。