如何在节点中获得当前页面的URL ?



我试图为一个网站建立一个评论部分,我希望每个评论都有一个静态URL,基本上是domain.com/titles/postID/commentID其中postID是评论回复的帖子ID。

下面是我的代码:

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中,它返回'/comment'这是post请求的url,而不是页面。

那么您应该尝试post到post ID,这是相当容易在Express中支持:

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

最新更新