1

grunt//webpackexpress,我可以将其他域/主机名中的一些 API 代理到提供 html 页面的当前服务器并解决 CORS 问题。

我找到了启动http服务器的figwheel用途ring,然后我想我可以使用https://github.com/tailrecursion/ring-proxyfigwheel服务器添加代理路径。但我不知道如何在figwheel项目之外做到这一点。

谢谢!

4

1 回答 1

0

我使用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) \/))))})))

app-routes改为handler.clj

(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'")will 就像您在请求(GET "https://www.example.com").

顺便说一句,这些代码中的大部分都是从https://github.com/tailrecursion/ring-proxy和其他一些类似的存储库中窃取的。

于 2017-05-27T08:00:03.353 回答