Cannot create Angular application http-server - Error: css and js files 404 Not found

Cannot create Angular application http-server - Error: css and js files 404 Not found

我想创建一个 PWA,但 http-server 包不起作用。

所以我用 ng new myProject 创建了一个默认的 Angular-App,然后用 ng build --prod

创建了 dist- 文件夹

现在一切就绪,所以我 cd 进入 dist 文件夹和 运行 npm 包 http-server -o 第一个站点打开,我点击 link 但后来我收到很多错误消息。该站点保持空白(完全白色),其中应该有 Angular CLI 的 Heroes-Demo。

[Wed Jul 03 2019 10:37:24 GMT+0200 (Central European Summer Time)] "GET /styles.3ff695c00d717f2d2a11.css" Error (404): "Not found"
[Wed Jul 03 2019 10:37:24 GMT+0200 (Central European Summer Time)] "GET /runtime-es2015.858f8dd898b75fe86926.js" Error (404): "Not found"
[Wed Jul 03 2019 10:37:24 GMT+0200 (Central European Summer Time)] "GET /polyfills-es2015.27661dfa98f6332c27dc.js" Error (404): "Not found"
[Wed Jul 03 2019 10:37:24 GMT+0200 (Central European Summer Time)] "GET /main-es2015.79588c7e85ca7a0dacf0.js" Error (404): "Not found"

所以我什至无法让网站正常运行,可能是什么问题?

这是整个日志:

$ http-server -o
Starting up http-server, serving ./
Available on:
  http://159.144.42.198:8080
  http://192.168.34.113:8080
  http://127.0.0.1:8080
Hit CTRL-C to stop the server
[Wed Jul 03 2019 10:37:24 GMT+0200 (Central European Summer Time)] "GET /styles.3ff695c00d717f2d2a11.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"
[Wed Jul 03 2019 10:37:24 GMT+0200 (Central European Summer Time)] "GET /runtime-es2015.858f8dd898b75fe86926.js" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"
[Wed Jul 03 2019 10:37:24 GMT+0200 (Central European Summer Time)] "GET /polyfills-es2015.27661dfa98f6332c27dc.js" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"
[Wed Jul 03 2019 10:37:24 GMT+0200 (Central European Summer Time)] "GET /main-es2015.79588c7e85ca7a0dacf0.js" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"
[Wed Jul 03 2019 10:37:24 GMT+0200 (Central European Summer Time)] "GET /styles.3ff695c00d717f2d2a11.css" Error (404): "Not found"
[Wed Jul 03 2019 10:37:24 GMT+0200 (Central European Summer Time)] "GET /runtime-es2015.858f8dd898b75fe86926.js" Error (404): "Not found"
[Wed Jul 03 2019 10:37:24 GMT+0200 (Central European Summer Time)] "GET /polyfills-es2015.27661dfa98f6332c27dc.js" Error (404): "Not found"
[Wed Jul 03 2019 10:37:24 GMT+0200 (Central European Summer Time)] "GET /main-es2015.79588c7e85ca7a0dacf0.js" Error (404): "Not found"

因为您的 angular 应用是一个 SPA,您需要将每个请求重新路由到 index.html

对于http-server可以使用代理参数,例如:

http-server --proxy http://localhost:8080?

http-serverREADME中也提到了。