如何在 angular 中使用代理解决 CORS 问题
How to resolve the CORS issue using proxy in angular
我正在为 angular 使用 express Node 后端和前端。问题是当我将 API 集成到 angular 时,我遇到了 CORS 问题。在开发人员控制台中,我遇到了这样的错误 "XmlHttpRequet cannot load http://localhost:3000/student No Access-Control-Allow-Origin"
我尝试在 angular 项目中添加一个代理-confg.json 来代理后端。但结果也一样。仍然有 CORS 问题。
// node js endpoint
ApienpointUrl = 'http://localhost:4200/stdApi/student';
// getting data from endpoint
getStudentfromMock() {
return this.http.get(this.ApienpointUrl).pipe(
map((res: any) => res),
catchError(this.handleError)
);
}
这里是angular项目里面的Proxy配置proxy.conf.json
{
"/stdApi/*": {
"target": "http://localhost:3000",
"secure": false,
"logLevel": "debug",
"changeOrigin": true
}
}
我收到这样的错误:-
"XmlHttpRequet cannot load http://localhost:4200/stdApi/student No Access-Control-Allow-Origin. header is present on the request resources origin 'http://localhost:3000' therefor not allowed access".
在 app.js 文件中添加此代码。
app.use((req, res, next) => {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With, content-type, token, language');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
并重启服务器。
为了代理到后端服务器,在项目的src/
文件夹中的proxy.conf.json
中添加代理配置后,将proxyConfig
选项添加到serve
目标,在CLI 配置文件,angular.json
:
...
"architect": {
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "your-application-name:build",
"proxyConfig": "src/proxy.conf.json"
},
...
然后 运行 使用此代理配置的开发服务器,调用 ng serve
。
或
运行 ng serve
代理配置:
ng serve --proxy-config proxy.conf.json
更多信息:https://angular.io/guide/build#proxying-to-a-backend-server
我找到了另一种修复此 CORS 的方法。这是什么。
重写 URL 路径
{
"/api": {
"target": "http://localhost:3000",
"secure": false,
"pathRewrite": {
"^/api": ""
}
}
}
这对我也有用。
我正在为 angular 使用 express Node 后端和前端。问题是当我将 API 集成到 angular 时,我遇到了 CORS 问题。在开发人员控制台中,我遇到了这样的错误 "XmlHttpRequet cannot load http://localhost:3000/student No Access-Control-Allow-Origin"
我尝试在 angular 项目中添加一个代理-confg.json 来代理后端。但结果也一样。仍然有 CORS 问题。
// node js endpoint
ApienpointUrl = 'http://localhost:4200/stdApi/student';
// getting data from endpoint
getStudentfromMock() {
return this.http.get(this.ApienpointUrl).pipe(
map((res: any) => res),
catchError(this.handleError)
);
}
这里是angular项目里面的Proxy配置proxy.conf.json
{
"/stdApi/*": {
"target": "http://localhost:3000",
"secure": false,
"logLevel": "debug",
"changeOrigin": true
}
}
我收到这样的错误:-
"XmlHttpRequet cannot load http://localhost:4200/stdApi/student No Access-Control-Allow-Origin. header is present on the request resources origin 'http://localhost:3000' therefor not allowed access".
在 app.js 文件中添加此代码。
app.use((req, res, next) => {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With, content-type, token, language');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
并重启服务器。
为了代理到后端服务器,在项目的src/
文件夹中的proxy.conf.json
中添加代理配置后,将proxyConfig
选项添加到serve
目标,在CLI 配置文件,angular.json
:
...
"architect": {
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "your-application-name:build",
"proxyConfig": "src/proxy.conf.json"
},
...
然后 运行 使用此代理配置的开发服务器,调用 ng serve
。
或
运行 ng serve
代理配置:
ng serve --proxy-config proxy.conf.json
更多信息:https://angular.io/guide/build#proxying-to-a-backend-server
我找到了另一种修复此 CORS 的方法。这是什么。
重写 URL 路径
{
"/api": {
"target": "http://localhost:3000",
"secure": false,
"pathRewrite": {
"^/api": ""
}
}
}
这对我也有用。