douwei1904 2017-09-30 06:29
浏览 47
已采纳

如何在同一域上运行Golan应用程序(后端)和ReactJS应用程序(前端)以防止CORS?

So, I have a golang application and reactJs application. If I run them on the different domains, I get problems with CORS.

To allow CORS on the server side is not a problem at all, but I can't get cookies from the server, because they are ignored by browser as third-party cookies.

So, I see quite a simple solution - to run server and frontend on the same domain, but I can't understand, how to do that. How could I configure everything to run on the same domain without conflicts? What are the best practices to prevent CORS between golang app and ReactJS app?

  • 写回答

1条回答 默认 最新

  • doufu5747 2017-10-02 15:05
    关注

    Use Go's file server to serve the static assets:

    package main
    
    import (
        "log"
        "net/http"
    )
    
    func main() {
        http.HandleFunc("/api/", apiHandler)
        http.Handle("/", http.FileServer(http.Dir("/var/www/my-app")))
        log.Fatal(http.ListenAndServe(":3000", nil))
    }
    
    func apiHandler(w http.ResponseWriter, r *http.Request) {}
    

    The documentation has more examples for http.FileServer.

    Alternatively, you can use something like github.com/jteeuwen/go-bindata (and go-bindata-assetfs) to compile your assets into the Go binary and serve them from memory.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?