2013-01-13 69 views
0

我想为一个网站建立一个评论部分,我希望每个评论都有一个基本上是domain.com/titles/postID/commentID的静态URL,其中postID是评论回复到的帖子的ID 。如何获取节点中当前页面的URL?

这里是我的代码:

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

    buildpost.comment(comment, username) 

}; 

这里的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); 

}; 

这里是我的app.js文件中的相关代码:

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

这里的玉石形式:

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

如果我只是在exports.commentHandler中沿着这些行使用req.url或其他东西,它会返回'/ comment',这是发布请求的URL,而不是页面。

回答

1

那么你应该尝试张贴的帖子ID,它很容易支持这个在快递:

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

我不知道我跟随。我只是将当前的发布请求更改为那个?我是否也将我的玉石形式的那部分做出来?我会张贴更多的代码。 – user1816679

+0

如果您忽略了

动作属性,表单将发布到网页提供的任何网址。因此,这个组合将处理这个:app.get('/ titles /:id',...);和app.post('/ titles /:id',...); – 7zark7

+0

完美地工作,谢谢! – user1816679