代码之家  ›  专栏  ›  技术社区  ›  Zazaeil

如何配置express以在生产中服务angular应用程序?

  •  0
  • Zazaeil  · 技术社区  · 6 年前

    客户端路由机制有一个常见的问题:一旦部署,就称为“深度链接”,如 host/deep/deeper/deepest/link/1 吐出404找不到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')));
    

    0 回复  |  直到 6 年前
        1
  •  1
  •   Shohel    6 年前
    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/mdshohelrana/mean-stack/blob/master/server/app.ts