0

I made an angular(2+) app hosted in aws ec2 using bitnami MEAN stack. Everything is working fine, except angular reloading which is giving 404. I know that angular is a SPA(Single Page Application), so we need to send index.html file everytime(that's why we're getting this error). So, I did some research and find this code for nodejs and I wrote this after all routes:

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

But, I got nothing to display in browser and what I got in browser console was:

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.

Note: I know about hashes in angular, and I don't wanna use those, I want some server configuration.

Satnam112
  • 242
  • 1
  • 10

1 Answers1

1

Your server needs rewrite rules to redirect. For apache server add .htaccess-file with:

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

For nginx try this: How to redirect all Angular request to index.html in Nginx

Kheder
  • 203
  • 3
  • 9
  • Thanks for your answer @Kheder. I write these rules in .htaccess file and when I restart apache server, I got error- AH00526: Syntax error on line 3 of /opt/bitnami/apps/myapp/conf/.htaccess: RewriteBase: only valid in per-directory config files apache config test fails, aborting – Satnam112 Nov 13 '19 at 12:11