我有一个 react 应用程序运行良好。我想使用快速静态来部署它。所以我已经给出了客户端中构建文件夹的快速静态路径。 '/' 路由工作正常,React 路由器从那里接手工作。但是当我刷新页面时,express无法发送构建文件,而是给出错误
尝试使用*所有路由到同一个 react 构建文件
app.use(express.static(path.join(__dirname, '../client/', 'build')));
app.get('*', function(req, res) {
res.sendFile('index.html', function(err) {
if (err) {
res.status(500).send(err);
}
});
});
与其再次在express中写下(复制)所有路线,不如想要一个更简单的解决方案
最佳答案
尝试此设置:
app.use(express.static(path.join(__dirname, 'build')));
app.get('*', function (req, res, next) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
关于javascript - 使用 React 应用程序的 Express 处理路线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57884679/