<%= title %>

By <%= author.name %>   edit

<%= content %>


<% if (comments.length == 0) { %> No comments <% } else { %>

Comments

<% } %> <% for(i=0; i

<%= comments[i].name %> said
<%= comments[i].text %>
<%= comments[i].date %>

<% } %>
Add a comment

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);
                    }

                })

            });
        

Code sample - how a comment is POST'd to the server

// web.js
// add a comment to a blog post

app.post('/comment', function(request, response){
    
    // get the comment form's hidden value - urlslug
    var urlslug = request.body.urlslug;
    
    // Query for the blog post with matching urlslug
    BlogPost.findOne({urlslug:urlslug}, function(err,post){
        
        // if there was an error...
        if (err) {
            console.log('There was an error');
            console.log(err);
            
            // display message to user
            response.send("uh oh, can't find that post"); 
        }
        
        // Prepare, save and redirect
        
        // prepare new comment for blog post with the form data
        var commentData = {
            name : request.body.name,
            text : request.body.text
        };
        
        // create new comment
        var comment = new Comment(commentData);
        
        // append the comment to the comment list
        post.comments.push(comment);
        post.save();
        
        // redirect to the blog entry
        response.redirect('/entry/' + urlslug);

    });
    
});