gpt4 book ai didi

javascript - 如何从 node.js 文件访问 javascript 变量

转载 作者:可可西里 更新时间:2023-11-01 11:37:33 27 4
gpt4 key购买 nike

这听起来可能很愚蠢,但我在 html 文件中有一个 javascript 脚本。我想使用 redis 存储 javascript 变量的值,但 redis 只与 node.js 兼容有没有办法从 node.js 访问 html 文件的脚本变量,然后将它们存储在 redis 中?

最佳答案

直接你不能。您可以对您的 Node 服务器进行 ajax 调用(http://api.jquery.com/jquery.ajax/),然后将您喜欢的变量保存在 redis(http://redis.io/commands/SET)中。

您可以选择的 ajax 调用取决于您究竟想做什么以及您的应用程序是否为 RESTful。假设你想向 redis 添加一个变量,POST 调用是最合适的。 (https://api.jquery.com/jquery.post/)。

举个例子,假设您将两个变量发布到您的 Node 。

$.post("save_to_redis",{var1 : "example_value_1", var2: "example_value_2"},function(result){
//do something with what is returned
}

然后在 Node 中(使用 express):

app.post('/save_to_redis',function(req,res){

var variable1 = req.body.var1;
var variable2 = req.body.var2;

//call save to redis for var1 , var2

res.send("whatever you like");
});

希望对你有帮助

关于javascript - 如何从 node.js 文件访问 javascript 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30171647/

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