Angular Proxy代理

1,263 阅读1分钟

proxy 代理是为了解决前端开发时跨域的问题

ng-proxy是基于webpack proxy webpack.js.org/config...

proxy.conf.json

{
    "/api": {
        "target": "http://localhost:8081/",
        "secure": false,
        "pathRewrite": {
            "^/api": ""
        },
        "changeOrigin": true,
        "loglevel": "debug"
    }
}

示例说明:

对所有/api/*的请求,转发到http://localhost:8081/

  • target,一般是后端服务地址

  • pathRewrite,重写url路径。效果就是前端统一请求后端时使用/api/,而实际代理时则无此关键字。

  • changeOrigin,如果你的后端服务不是localhost,则需把此项设为true

  • loglevel,日志级别。包括 debug, info, warn, error, and silent (默认 info)

启动代理

ng serve --proxy-config proxy.conf.json