angular - 如何配置 express 以在生产中提供 Angular 应用程序?
问题描述
客户端路由机制存在一个常见问题:一旦部署,所谓的“深层链接”host/deep/deeper/deepest/link/1
会吐出 404 Not Found rahter,然后传播到index.html
特定组件并正确解析。
我该如何配置它?
我试过:
app.use(express.static(root)); // root folder of the project
app.use((req, res) => res.sendFile(path.join(__dirname, root,'index.html')));
...但它仅适用于基本网址;所有其他人都被拒绝。
解决方案
app.use(express.static(root)); // root folder of the project
这意味着没有挂载路径的中间件功能。对路由器的每个请求都执行此代码
尝试使用看起来像这样
app.use('/', express.static(path.join(__dirname, '../root')));
app.get('/*', function(req, res) {
res.sendFile(path.join(__dirname, '../root/index.html'));
});
完整的示例在这里 https://github.com/mdshohlrana/mean-stack/blob/master/server/app.ts
推荐阅读
- java - 由于防止类加载器泄漏和 c3p0,tomcat 无法正常关闭
- javascript - 为什么我们在打字稿中使用 HTMLInputElement?
- laravel - Laravel 5.3 仅在同一个表中的另一行不存在时才返回结果
- xaml - Xamarin - ios xaml 预览失败并出现单点触控错误
- php - 无效的单元格坐标错误phpExcel - ?
- parsing - 如何遍历yacc生成的解析树?
- spring-boot - 为特定的 Tomcat Web 应用程序启动传递命令行参数
- ruby-on-rails - rails:嵌套资源保存空记录
- javascript - base64 或十六进制编码的 Int8 二进制字符串到 Int32Array
- android - 为什么我的 Admob 激励视频广告一次又一次加载失败?