Step 1: 在根目录下创建一个 proxy.conf.json
文件
{
"/api/*": {
"target": "http://localhost:3000",
"secure": false,
"logLevel": "debug",
"changeOrigin": true
}
}
Step 2: 在 package.json
文件中的 start
script
命令中添加 "--proxy-config proxy.conf.json"
"scripts": {
"ng": "ng",
"start": "ng serve --proxy-config proxy.conf.json",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
Step 3: 重启应用程序
ng serve
现在,你的代理将通过localhost:4200/api
转发到 localhost:3000/api
。请确保这两个 URL 是正确的,并确保代理服务器正在侦听传入的请求。