I am trying to send data from MongoDB through a redirect using Express.
I am fairly new to working with the server and connecting it to a front-end, so please be easy on me if I have any huge misunderstandings.
Here is an example of my code and what I am trying to do:
app.get('/dashboard', requireLogin, function(req, res) {
res.render('dashboard.jade');
});
app.get('/api/posts',function(req, res) {
return Post.find(function(err, posts) {
if (!err) {
console.log('Retrieving all posts.');
res.send(posts);
res.redirect('/dashboard');
} else {
return console.log(err);
}
});
});
When I render dashboard.jade
I have data that is being used in the template to display posts. What I'd like to do is make any GET calls to /api/posts
retrieve the posts data from Mongo and send it to the route /dashboard
with the post data it retrieved.