gpt4 book ai didi

javascript - Express.js : Get user's referer url when they post

转载 作者:行者123 更新时间:2023-11-28 06:01:35 24 4
gpt4 key购买 nike

所以我想为我的网站创建一个评论系统,为了知道评论的页面(例如,用户上传的特定视频),我需要以某种方式获取用户页面的url目前,当他们发表评论时(这样我就知道如何将其保存在数据库中)。

路线如下:app.get('/video/:videoId', function(req, res){...}) 因此视频网址将类似于:/video/98ux8987s987f9xc89v3wjgrkgh32

当用户发布评论时,我需要获取网址的最后一部分。 我不想将网址与评论一起发送(在 ajax POST 函数中),因为他们可以更改它!

顺便说一下,我不需要 url post url(如果我在 post 函数中有类似 req.url 的内容,我只需获取 post url:app .post('/comment', function(req, res){console.log(req.url)} 如果我这样做,我会得到 /comment)。

有办法吗?

非常感谢。

最佳答案

您可以使用 url 添加哈希值,但这不是不可阻挡的。在videoKey中,您可以添加日期或用户 ID。

var secretkey = "4658{=#mkZl"; // The user doesn't know this string, and he can't make videoKey.

app.get('/video/:videoId', function(req, res){
res.render('video', {
videoId: req.params.videoId,
videoKey: sha256(secretkey + req.params.videoId + secretkey);
});
};

在您的 html 表单中:

<form action="/comment">
<input type="hidden" name="videoId" value="{videoId}" />
<input type="hidden" name="videoKey" value="{videoKey}" />
</form>

评论功能:

var secretkey = "4658{=#mkZl";

app.post('/comment', function(req, res){
if (req.body.videoKey == sha256(secretkey + req.body.videoId + secretkey))
//ok the user got video page, before he comments.
};

sha256 function :

var crypto = require('crypto');

function sha256(data) {
return crypto.createHash("sha256").update(data).digest("base64");
}

关于javascript - Express.js : Get user's referer url when they post,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37227052/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com