在 NodeJs 服务器上运行 2 个静态 Angular 应用程序不起作用

问题描述 投票:0回答:1

我最近将 2 个 Angular 应用程序合并在一起。这两个应用程序都位于同一项目目录的单独源文件夹中。我已经更新了 angular.json 文件以分隔每个产品构建,并使用 ng build appName 来构建这两个应用程序。

我想在nodeJs服务器上服务器2个角度应用程序,比如说project1和project2,服务器将默认为第一个角度项目project1,我可以直接从project1导航到project2。

我遇到的问题是,如果我托管一个静态应用程序(如下所示),它就可以工作:

app.use(express.static(path.join(__dirname, '/dist/project1'), { dotfiles: 'allow' }));

app.get('*', function(req,res) {
  res.sendFile(path.join(__dirname+'/dist/project1/index.html'));
});

问题是,一旦我为第二个项目添加相同的内容,如下所示,页面不会加载,而是显示白色页面。

app.use(express.static(path.join(__dirname, '/dist/project2'), { dotfiles: 'allow' }));

app.get('*', function(req,res) {
  res.sendFile(path.join(__dirname+'/dist/project2/index.html'));
});

我还尝试为每个静态目录使用不同的路由,如下所示,但仍然没有运气。

app.use('/project1',express.static(path.join(__dirname, '/dist/project1'), { dotfiles: 'allow' }));
app.use('/project2',express.static(path.join(__dirname, '/dist/project2'), { dotfiles: 'allow' }));

当我只运行一个项目时,它可以工作,但是一旦我包含另一个项目,我就会得到一张空白的白页。除了白屏之外,我有时还会看到这些错误:

Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.
polyfills-es2015.23da8a2c52ef7f5fb136.js/:1 Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.
main-es2015.719b20337c641b8633cf.js/:1 Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.
node.js angular express hosting static-files
1个回答
0
投票

解决方法是更改一个项目的 index.html 文件的名称,并将 NodeJs 中的静态文件提供为:

app.use('/project1',express.static(path.join(__dirname, '/dist/project1'), { dotfiles: 'allow' }));

app.use(express.static(path.join(__dirname, '/dist/project2'), { dotfiles: 'allow' }));

    app.get('/project2', function(req,res) {
  res.sendFile(path.join(__dirname+'/dist/project2/index.html'));
});
app.get('*', function(req,res) {
  res.sendFile(path.join(__dirname+'/dist/project1/project1.html'));
});

index.html 名称中似乎有一个 - 导致其无法正确加载。

© www.soinside.com 2019 - 2024. All rights reserved.