Reactjs 在生产中创建React应用程序:未找到路由
我有一个fullstack应用程序,其后端是Create React app和Express.js 开发设置(CRA在端口3000上运行)由CRA的代理实现,因此我可以将某些路由直接转发到后端(在端口5000上运行): 对于注销用户,我有相应的路径:Reactjs 在生产中创建React应用程序:未找到路由,reactjs,express,heroku,proxy,create-react-app,Reactjs,Express,Heroku,Proxy,Create React App,我有一个fullstack应用程序,其后端是Create React app和Express.js 开发设置(CRA在端口3000上运行)由CRA的代理实现,因此我可以将某些路由直接转发到后端(在端口5000上运行): 对于注销用户,我有相应的路径: app.get('/api/logout', (req, res) => { req.logout(); res.redirect('/'); }); 以及一个指向此路线的注销按钮: <a key="logout" h
app.get('/api/logout', (req, res) => {
req.logout();
res.redirect('/');
});
以及一个指向此路线的注销按钮:
<a key="logout" href="/api/logout">Logout</a>
如果我通过调用相同的路由onClick
,以编程方式注销用户,则它可以工作:
await axios.get('/api/logout');
但为什么浏览器的简单重定向在生产环境中不起作用
谢谢 变化
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
const path = require('path');
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
为此:
let root = path.join(__dirname, 'client', 'build');
app.use(express.static(root));
app.use(function(req, res, next) {
if (req.method === 'GET' && req.accepts('html') && !req.is('json') &&
!req.path.includes('.')) {
res.sendFile('index.html', { root });
} else next();
});
也许这有助于:
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
const path = require('path');
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
let root = path.join(__dirname, 'client', 'build');
app.use(express.static(root));
app.use(function(req, res, next) {
if (req.method === 'GET' && req.accepts('html') && !req.is('json') &&
!req.path.includes('.')) {
res.sendFile('index.html', { root });
} else next();
});