duanhe1965 2018-02-16 01:49
浏览 40

如何使用Golang REST API正确设置Angular前端

I have a REST API written in Golang and front-end written in Angular running locally. The API is running on localhost:8000 and the Angular app is running on localhost:4200. My Angular application makes frequent GET/POST/DELETE/PUT calls to my API. I would like to keep my API and Angular app separated so that if later I want to add Android or iOS client, I can easily do so with my existing API.

The problem that I have is that both the server and web browser prevent me from CORS, especially for POST methods, and I have taken following action for local development:

  • Add Access-Control-Allow-Origin: * to my Golang API response headers.
  • Run Chromium browser with web security disabled (otherwise the browser is going to convert POST to OPTIONS): $ chromium --disable-web-security --user-data-dir

With the settings above, I can run my Angular app on localhost:4200 and call my API at localhost:8000 without issue.

I want to know how I should set up this in production.

My API will be deployed at https://api.myapp.mywebsite.com, for example, and my Angular app will be deployed at https://myapp.mywebsite.com (and they will most likely to be on different physical servers and IP). While I can still make my API accept calls from other origins, I cannot make all my clients running my Angular app in unsafe mode.

What am I missing here?

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 微信公众号自制会员卡没有收款渠道啊
    • ¥15 stable diffusion
    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
    • ¥15 perl MISA分析p3_in脚本出错
    • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
    • ¥15 ubuntu虚拟机打包apk错误
    • ¥199 rust编程架构设计的方案 有偿