Code sample - how a single blog post is displayed
// web.js
// Display a single blog post
// A user might request /entry/this-blog-is-great
//
// this-blog-is-great will be the variable :urlslug => request.params.urlslug
app.get('/entry/:urlslug',function(request, response){
// Get the request blog post by urlslug
BlogPost.findOne({urlslug:request.params.urlslug},function(err,post){
if (err) {
console.log('error');
console.log(err);
response.send("uh oh, can't find that post");
}
// use different layout for single entry view
post.layout = 'layout_single_entry.html';
// found the blogpost
response.render('blog_single_entry.html', post);
});
});
How to get blog post by _id
app.get("/entryById/:postId", function(request, response) {
var requestedPostID = request.params.postId;
BlogPost.findById( requestedPostID, function(err, blogpost) {
if (err) {
console.log(err);
response.send("an error occurred!");
}
if (blogpost == null ) {
console.log('post not found');
response.send("uh oh, can't find that post");
} else {
// use different layout for single entry view
blogpost.layout = 'layout_single_entry.html';
// found the blogpost
response.render('blog_single_entry.html', blogpost);
}
})
});
<%= comments[i].name %> said
<%= comments[i].text %>
<%= comments[i].date %>