2013-01-13 68 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