0

I am building my angular project and inserting the build output into a public folder in the root of my node project.

In my app.js file, I have the following code:

...
app.use(express.static(path.join(__dirname, 'public')));
...
app.get('*', (req, res) => {
     res.send(path.join(__dirname, 'public/index.html'));
});

When deploying the project, this works perfectly; when I load the page, the index.html is rendered and navigating through the page works.

However, when I enter a url like for example '/admin', which is defined in my angular routing, I get a not found error.

How can this be solved?

4 Answers 4

1

The problem was in the .send() method... I replaced this:

app.get('*', (req, res) => {
 res.send(path.join(__dirname, 'public/index.html'));
});

with this:

app.get('*', (req, res) => {
 res.sendFile(path.join(__dirname, 'public/index.html'));
});

and it worked. Sorry guys for making you loose time. You helped me research different things though!

Sign up to request clarification or add additional context in comments.

Comments

0

Hope this will solve ur issue Create .htaccess file in root folder and pass

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

RewriteRule ^(.*) /index.html [NC,L]

4 Comments

I've seen this solution with some differences.
Options -MultiViews RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.html [QSA,L]
What exactly does this do? Haven't tried it yet, will do now.
.htaccess is a configuration file for use on web servers running the Apache Web Server software. .htaccess files can be used to alter the configuration of the Apache Web Server software to enable/disable additional functionality and features that the Apache Web Server software has to offer. These facilities include basic redirect functionality, for instance if a 404 file not found error occurs
0

above router.get, you should have :-

// serve angular front end files from root path
router.use('/', express.static('public', { redirect: false }));

Comments

0

I don't know your code but

  1. you can try setting up the base-href in your index.html

    <base href="/"> 
    

    properly....

  2. Serving with nginx could have a configuration like this. It directs the index.html to the correct routes of your SPA:

    root /home/app/dist/;
    
    server {
        listen 8080;
        server_name  localhost;
    
        location /admin/ {
          alias /home/app/dist/my-app/admin/;
          expires -1;
          add_header Cache-Control "no-cache, must-revalicate, post-check=0 pre-check=0";
          index index.html;
          try_files $uri $uri/ index.html =404;
        }
    
        location / {
          alias /home/app/dist/my-app/;
          expires -1;
          add_header Cache-Control "no-cache, must-revalicate, post-check=0 pre-check=0";
          index index.html;
          try_files $uri $uri/ index.html =404;
        }
    }
    

see nginx how-to here: https://medium.com/better-programming/how-to-properly-get-angular-and-nginx-working-together-for-development-3e5d158734bf

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.