简体   繁体   English

Angular 13 模块联合 IIS 托管为 MF 提供 CORS 错误

[英]Angular 13 module federation IIS hosting give CORS error for MFs

I have a shell app [angular 13] hosted on my local IIS on port 2000 and a MF app which is hosted on IIS on port 1001. I have loaded my MF within shell app using dynamic module federation in shell route.我有一个 shell 应用程序 [angular 13] 托管在我本地 IIS 端口 2000 上,还有一个 MF 应用程序托管在 IIS 端口 1001 上。我已经在 shell 应用程序中使用 shell 中的动态模块联合加载了我的 MF route.981

    const routes: Routes = [
  {
    path: '',
    component: HomeComponent,
    children: [
      {
        path: '',
        outlet: 'pChild',
        loadChildren: () =>
          loadRemoteModule({
            type: 'module',
            remoteEntry: 'http://localhost:1001/remoteEntry.js',
            exposedModule: './AppModule',
          })
            .then((m) => {
              return m.AppModule;
            })
            .catch((e) => {
              return import('src/app/placeholder/error.module').then(
                (m) => m.ErrorModule
              );
            }),
      }]

I am getting CORS error for MF app.我收到 MF 应用程序的 CORS 错误。

Access to script at 'http://localhost:1001/remoteEntry.js' from origin 'http://localhost:2000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. CORS 策略已阻止从源“http://localhost:2000”访问“http://localhost:1001/remoteEntry.js”中的脚本:没有“Access-Control-Allow-Origin”header 出现在请求的资源。

UPDATED更新
Proxy.conf.json代理.conf.json

{
"/localhost/": {
    "target": "http://localhost:2000/",
    "changeOrigin": true,
    "logLevel": "debug"
}

} }

and below are setting proxy in angular.json.以下是在 angular.json 中设置代理。

"serve": {
                    "builder": "ngx-build-plus:dev-server",
                    "configurations": {
                        "production": {
                            "browserTarget": "shell:build:production",
                            "extraWebpackConfig": "webpack.prod.config.js",
                            "proxyConfig" : "src/proxy.conf.json"
                        },
                        "development": {
                            "browserTarget": "shell:build:development"
                        }
                    },

I had the same error.我有同样的错误。 The issue in my case was that my remote app was a devServer ( https://webpack.js.org/configuration/dev-server/ ) and under its webpack.config.js the cors configuration missed.我的问题是我的远程应用程序是一个 devServer( https://webpack.js.org/configuration/dev-server/ )并且在它的webpack.config.js下缺少 cors 配置。

devServer: {
  ...,
  headers: {
    "Access-Control-Allow-Origin": "*",
    "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, PATCH, OPTIONS",
    "Access-Control-Allow-Headers": "X-Requested-With, content-type, Authorization"
  }
},

Finally found a solution for this.终于找到了解决方案。

Go to IIS, click on your website, find the HTTP RESPONSE HEADERS icon, and configure it like this: Go 到 IIS,点击你的网站,找到HTTP RESPONSE HEADERS图标,然后这样配置:

在此处输入图像描述

After that, there is a chance you still get an 404 error while trying to fetch a ".mjs" file, if that is the case, just add it to the mime types like this:之后,在尝试获取“.mjs”文件时仍有可能出现 404 错误,如果是这种情况,只需将其添加到 mime 类型中,如下所示:

在此处输入图像描述

And that should solve your issue.那应该可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM