user1816679
user1816679

Reputation: 855

How do I get the URL of the current page in node?

I'm trying to build a comments section for a site and I want each comment to have a static URL that is basically domain.com/titles/postID/commentID where postID is the ID of the post the comment is in reply to.

Here's my code:

exports.commentHandler = function(req, res){
    comment = req.body.comment;
    username = req.session.username;

    buildpost.comment(comment, username)

};

Here's buildpost.comment:

exports.comment = function(comment, username){
    var id = makeid(7);

    var comment = {comment: comment, user: username, postID: id, type: "comment"}

    console.log(comment);
    postdb.write(comment);

};

Here is the relevant code from my app.js file:

app.get('/titles/:id', home.content); //this serves the post's page, where the comments are
app.post('/comment', home.commentHandler);

Here's the jade form:

form.sbBox#commentReply(method='post', action='/comment')

If I just use req.url or something along those lines in exports.commentHandler, it returns '/comment' which is the URL of the post request and not the page.

Upvotes: 0

Views: 2991

Answers (1)

7zark7
7zark7

Reputation: 10145

Then you should try POSTing to the post ID , it's fairly easy to support this in Express:

app.post('/:postId/comments', function(req, res) {
  var postId = req.params[postId]
  ...
})

Upvotes: 1

Related Questions