【问题标题】:How to do proxy in `figwheel` dev server?如何在“figwheel”开发服务器中进行代理?
【发布时间】:2017-05-17 09:29:48
【问题描述】:

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

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

谢谢!

【问题讨论】:

    标签: clojurescript figwheel


    【解决方案1】:

    我使用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 和其他一些类似的存储库中窃取的。

    【讨论】:

      猜你喜欢
      • 2017-09-12
      • 2021-04-10
      • 2018-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-01
      • 2012-11-11
      相关资源
      最近更新 更多