在生产服务器中重新加载Angular应用程序会显示404

我使用bitnami MEAN堆栈制作了一个由AWS EC2托管的angular(2+)应用程序。一切正常,除了提供404的角度重装。我知道angular是SPA(单页应用程序),因此我们每次都需要发送index.html文件(这就是为什么会出现此错误)。因此,我进行了一些研究,找到了用于nodejs的代码,并在所有路径后编写了该代码:

app.use('*',(req,res,next) => {
 res.sendFile(filePath);
}) 

但是,我在浏览器中什么也没显示,在浏览器控制台中显示的是:

Failed to load module script: The server responded with a non-JavaScript MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.

注意:我了解有角度的哈希,但我不想使用这些哈希,我需要一些服务器配置。

z156578453 回答:在生产服务器中重新加载Angular应用程序会显示404

您的服务器需要重写规则才能重定向。 对于apache服务器,请添加.htaccess文件,

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.html [L]
</IfModule>

对于nginx,请尝试以下操作:How to redirect all Angular request to index.html in Nginx

本文链接:https://www.f2er.com/3112371.html

大家都在问