【问题标题】:How to serve a react app on express subdomain?如何在快速子域上提供反应应用程序?
【发布时间】:2017-09-04 12:13:45
【问题描述】:

我们有一个单页 React 应用程序,我们目前正在一个公共端点(如 awesomewebsite.com/app )中托管。它最初确实有效,但如果刷新,服务器将重定向到 404 页面。我们通过在主路由器下包含通配符路由解决了这个问题。我们现在想把整个东西带到一个子域,但在那里应用同样的技巧似乎并没有成功。

当在公共场合服务时,这一项有效

  app.use(express.static(path.join(__dirname, 'public')));
  app.use('/webapp/*',function(req,res){
    res.sendFile(path.join(__dirname,'public/webapp/index.html'))
})

但是这个没有

app.use(subdomain('app', express.static(path.join(__dirname, 'reactApp'))));
app.use(subdomain('app', express.Router().use('/*',function(req,res){
    res.sendFile(path.join(__dirname,'reactApp/index.html'))
})));

任何人都知道为什么会发生这种情况以及我们如何使用 express-subdomain 来解决它?在此先感谢您。

【问题讨论】:

    标签: node.js express react-router subdomain


    【解决方案1】:

    如果你合并它们,它应该可以工作。

    app.use(subdomain('app', express.Router()
        .use(express.static(path.join(__dirname, 'reactApp')))
        .use('/',function(req,res){
             res.sendFile(path.join(__dirname, 'reactApp/index.html'))
            //res.sendFile(path.join(__dirname,'public/webappbeta/index.html'))
        })
    ));
    

    【讨论】:

      猜你喜欢
      • 2014-12-12
      • 2021-06-11
      • 1970-01-01
      • 1970-01-01
      • 2019-04-05
      • 1970-01-01
      • 2018-10-30
      • 1970-01-01
      • 2011-06-05
      相关资源
      最近更新 更多