【发布时间】:2019-06-15 15:43:20
【问题描述】:
我一直在研究如何在 NodeJS Express 服务器上部署我的 Angular 6 项目,
首先,在开发中,我使用ng serve,它指的是localhost:4200(默认),另一个是localhost:3000 上的Node Express for API(与DB 交互)。在生产中,我也希望从该 Node Express 服务器提供 Angular 构建。
所以我做的是:
- 在 Angular 项目的
index.html上设置<base href="/"> - 运行
ng build --prod100% 顺利,没有错误。 - 将Angular上
dist/myprojectname中的所有文件复制到views/下的Node Express服务器目录。 - 在
index.js我添加以下行app.use(express.static(path.join(__dirname, '/views/')));
出现类似这样的错误
Refused to apply style from 'http://localhost:3001/styles.a64e6aa0f6090e05d2190.css/' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
3localhost/:16 GET http://localhost:3001/runtime.16a329deb1d564eef6599.js/ net::ERR_ABORTED 404 (Not Found)
如果我使用app.use('/*', express.static(path.join(__dirname, '/views/')));
它会给出以下错误:
Uncaught SyntaxError: Unexpected token <
【问题讨论】: