如何在 express 子域上提供 React 应用程序服务?
How to serve a react app on express subdomain?
我们有一个单页 React 应用程序,目前托管在一个 public 端点,如 awesomewebsite.com/app 。它最初确实有效,但如果刷新,服务器将重定向到 404 页面。我们通过在主路由器下包含一个通配符路由来解决这个问题。我们现在想把整个东西带到一个子域,但在那里应用同样的技巧似乎并没有成功。
当在 public 下服务时,这个有效
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 解决它?在此先致谢。
如果合并它们,应该可以。
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'))
})
));
我们有一个单页 React 应用程序,目前托管在一个 public 端点,如 awesomewebsite.com/app 。它最初确实有效,但如果刷新,服务器将重定向到 404 页面。我们通过在主路由器下包含一个通配符路由来解决这个问题。我们现在想把整个东西带到一个子域,但在那里应用同样的技巧似乎并没有成功。
当在 public 下服务时,这个有效
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 解决它?在此先致谢。
如果合并它们,应该可以。
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'))
})
));