简体   繁体   中英

How can I run Angular in a production environment?

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

This serves the Angular application fine. Link clicking works. Although if I enter the URL with a /route-name directly as in url.com/route-name , the app crashes.

I was using lite-server but the lite-server docs recommended to only use it in development. When using lite-server , I can enter a URL with a route directly and it works fine.

How can I configure http-server to be able to enter a URL address with a page route or what is another good way to run Angular in a production environment?

As mzulch said, you need a server that can redirect url that are not found to index.html. You fail to load an url because angular router is not loaded when you enter this url. So it cannot intercept requests and the browser displays a 404 not found error. lite-server is a perfect server to test during development.

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

In the nginx configuration you can define this like that :

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

This will redirect all 404 error to your index.html and transform 404 error code to 200.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM