从 React 应用程序路由以获取服务器端渲染 HTML 从车把生成
Routing from a react app to get a server side rendered HTML generated from handlebars
我的 React 应用程序 运行 localhost:3000,我的节点服务器 运行 localhost:5000。
我的 React 应用程序中有一个名为 Book 的按钮,路径为 localhost:3000/。单击按钮时,我想呈现路由 localhost:5000/book 呈现的页面(由 handlebars 模板引擎生成)。
其实我现在很迷茫。我知道 localhost:5000/book 会呈现页面。但是,当从 localhost:3000/.
的反应代码中单击图书按钮时,我如何呈现该页面
我知道,我不能 href 到 localhost:3000/book,因为它是 React 的 url 而不是服务器的 url。
因此,我试图通过 axios 将页面作为请求获取。但它不起作用。 (我知道我做错了)。
关键是我应该如何路由到 localhost:5000/book 并在我单击图书按钮时通过我的 React 应用程序呈现它?
我的节点代码
app.engine('handlebars', exphbs());
app.set('view engine', 'handlebars');
// Routes
app.get('/book', (req, res) => {
res.render('book');
})
您可以在点击 book button
时添加 redirect
处理程序
redirect = () => {
window.location.href = 'http://localhost:5000/book';
return null;
}
我的 React 应用程序 运行 localhost:3000,我的节点服务器 运行 localhost:5000。
我的 React 应用程序中有一个名为 Book 的按钮,路径为 localhost:3000/。单击按钮时,我想呈现路由 localhost:5000/book 呈现的页面(由 handlebars 模板引擎生成)。
其实我现在很迷茫。我知道 localhost:5000/book 会呈现页面。但是,当从 localhost:3000/.
的反应代码中单击图书按钮时,我如何呈现该页面我知道,我不能 href 到 localhost:3000/book,因为它是 React 的 url 而不是服务器的 url。 因此,我试图通过 axios 将页面作为请求获取。但它不起作用。 (我知道我做错了)。
关键是我应该如何路由到 localhost:5000/book 并在我单击图书按钮时通过我的 React 应用程序呈现它?
我的节点代码
app.engine('handlebars', exphbs());
app.set('view engine', 'handlebars');
// Routes
app.get('/book', (req, res) => {
res.render('book');
})
您可以在点击 book button
redirect
处理程序
redirect = () => {
window.location.href = 'http://localhost:5000/book';
return null;
}