如何在我的proxy.conf.json中定义代理的多个路径?github上的angular-cli proxy documentation看起来只能有一条路径(/api):
{
"/api": {
"target": "http://localhost:3000",
"secure": false
}
}但是当我查看webpack proxy或http-proxy-middleware文档时,我发现应该可以定义多个路径(/api-v1和/api-v2):
// Multiple entry
proxy: [
{
context: ['/api-v1/**', '/api-v2/**'],
target: 'https://other-server.example.com',
secure: false
}
]但我不明白怎么才能把这个放到proxy.conf.json里。
发布于 2017-06-12 15:47:05
在proxy.conf.json中使用以下语法:
[
{
"context": ["/api-v1/**", "/api-v2/**"],
"target": "https://other-server.example.com",
"secure": false
}
]实际有效的语法如下:
[
{
"context": [
"/api",
"/other-uri"
],
"target": "http://localhost:8080",
"secure": false
}
]发布于 2018-06-15 17:33:09
多个条目的语法(使用上下文)记录在这里:https://github.com/angular/angular-cli/blob/master/docs/documentation/stories/proxy.md#multiple-entries
const PROXY_CONFIG = [
{
context: [
"/my",
"/many",
"/endpoints",
"/i",
"/need",
"/to",
"/proxy"
],
target: "http://localhost:3000",
secure: false
}
]
module.exports = PROXY_CONFIG;这还要求您将配置从.json重命名为.js,并将运行命令指向新文件。
对我来说,上下文语法并不能很好地工作(我假设是因为我想使用通配符)。因此,我想出了以下解决方案,允许您生成配置:
module.exports = [
"/my",
"/many",
"/endpoints",
"/i",
"/need",
"/to",
"/proxy",
"/folder/*"
].reduce(function (config, src) {
config[src] = {
"target": "http://localhost:3000",
"secure": false
};
return config;
}, {});这就完成了我的工作。(请注意,这仍然需要您将proxy.conf.json重命名为proxy.conf.js,并编辑您的运行命令以指向重命名的文件)
发布于 2021-03-23 00:16:47
截至2021-03年度,答案如下:
angular.json中,添加/修改代理文件:..。"architect":{ "serve":{ "builder":"@angular-devkit/build-angular:dev-server","options":{ "browserTarget":“您的应用程序名称:build”,"proxyConfig":"src/proxy.conf.js“},...
proxy.conf.js,如下所示:const PROXY_CONFIG =[{ context:"/my","/many","/endpoints","/i","/need","/to","/proxy“,目标:"http://localhost:3000",PROXY_CONFIG: false }]secure=secure;
注意,它是.js,不是.json。
更新,2021-07-08 It takes .js or .json。前者更好,因为它允许// comment。
对于SSL:
"target" : "https://localhost:3001",
"changeOrigin": true, // solves CORS Error in F12
"logLevel": "debug", //"info": prints out in console
"rejectUnauthorzied": true, // must be false if not specify here
"secure": false, // PROD must be "true", but DEV false else "UNABLE_TO_VERIFY_LEAF_SIGNATURE"
"strictSSL": true, // must be false if not specify here
"withCredentials": true // required for Angular to send in cookiehttps://stackoverflow.com/questions/44494012
复制相似问题