How to run Angular in a production environment?

I am currently working with Angular from a server with http-server using the following command: http-server./index.html .

This is great for an Angular app. Clicking on the link works. Although, if I enter the URL with /route-name directly, as in url.com/route-name , the application crashes.

I used lite-server but the lite-server documentation recommended using it only in development. When using lite-server I can directly enter the URL with route and it works fine.

How can I configure http-server so that it can enter a URL with a page route, or what other good way to run Angular in a production environment?

+5
source share
1 answer

As mzulch said, you need a server that can redirect a URL that is not found in index.html. You are not loading the url because the angular router is not loading when you enter this url. Thus, it cannot intercept requests, and the browser displays 404 not found error. lite-server is an ideal server for testing during development.

During production, you can configure the nginx server to redirect all 404 requests to index.html, and then the responsibility of your angular router to handle real 404

In the nginx configuration, you can define this as follows:

 server { error_page 404 =200 /index.html # other conf } 

This will redirect the entire 404 error to your index.html and convert the 404 error code to 200.

0
source

Source: https://habr.com/ru/post/1240894/


All Articles