I have a simple express server with a connection to a orientdb database. I need to pass information from express to react views. For example, in express I have:
router.get('/', function(req, res, next) {
Vertex.getFromClass('Post').then(
function (posts) {
res.render('index', { title: 'express' });
}
);
});
So, in this example, I need to have in my react index component, the posts
variable to set the state of the componenent. (I'm using react only in the frontend, not server-side)
class IndexPage extends React.Component {
constructor(props) {
super(props);
this.state = {
posts: []
};
}
render() {
return (
<div>
<Posts posts={posts} />
</div>
);
}
}
How can I get the posts in react from express?
I found that maybe I can do an ajax request from react, but I think that that isn't the best way.
If I need to get that posts in a real time way, using socket.io for example, what are the differences?
PD: In express I have the possibility to use some template engine like handlebars or hogan. Can this template engines help in this topic?
Thanks!!!