2017-06-21 107 views
0

在Redis的运行Lua中我经历了这样的命令行运行Lua中: -执行Redis的eval命令中的NodeJS

$ redis-cli --eval debug_script.lua key1 key2 key3 key4 , arg1 arg2 

所以,我的Lua脚本接受4个按键和2个参数。

现在我想在Node.js中运行相同的脚本。

我正在使用this库在我的应用程序中导入Redis。

我没有找到任何例子来说明执行Lua脚本的redisClient.eval(...)函数的参数。

因此,我只是打了一些随机可能工作。但似乎没有任何工作。

我app.js是这样的:

var redis = require("redis") 
var client = redis.createClient(); 

// my hit and trial guess 
client.eval(["script_file.lua", 1 "score" 0 10 , "meeting_type:email" meeting_status:close], function(err, res){ 
    console.log("something happened\n"); 
}); 

我的问题:如何使用Node.js,因为它确实在通过CLI(命令行执行的如何执行下面的命令,所以它返回同样的事情-接口)。

$ redis-cli --eval debug_script.lua key1 key2 key3 key4 , arg1 arg2 

回答

0

发现了一些解决方案:

溶液1

var redis = require('redis') 
var client = redis.createClient() 
var fs = require('fs') 

client.eval(fs.readFileSync('./debug_script.lua'), 4, key1, key2, key3, key4, arg1, arg2, function(err, res) { 
    console.log(res); 
}); 

4(EVAL的第二个参数)表示在脚本要传递的键的数目。

解决方案2)创建子进程并运行CLI命令。

var redis = require("redis"); 
var client = redis.createClient(); 

var exec = require('child_process').exec; 

var cmd = 'redis-cli --eval debug_script.lua key1 key2 key3 key4 , arg1 arg2'; 


exec(cmd, function(error, stdout, stderr) { 
    // command output is in stdout 
     console.log("something happened \n"); 
     console.log(stdout); 
    });