In Redis I run a Lua script through CLI like this:-
$ redis-cli --eval debug_script.lua key1 key2 key3 key4 , arg1 arg2
So, my Lua script accepts 4 keys and 2 arguments.
Now I want to run the same script in Node.js.
I am using this library for importing Redis in my app.
I didn't find any example which tells about the arguments of redisClient.eval(...) function for executing the Lua script.
Thus I am just hitting something random that might work. But nothing seems to work.
My question: How to execute below command using node.js, so that it returns the same thing as it does when executed through CLI(command-line-interface).
$ redis-cli --eval debug_script.lua key1 key2 key3 key4 , arg1 arg2
--eval
is no 'real' redis command but just an extra option of redis-cli. However it's still possible to do what you want:
'use strict'
const redis = require('redis')
const client = redis.createClient()
const fs = require('fs')
client.eval(fs.readFileSync('./test.lua'), 2, 'key1', 'key2', 'first', 'second', function(err, res) {
console.log(arguments);
});
Keep in mind that you might wanna do some security checks and that EVAL
requires more options than --eval
. Read more about the difference between --eval
and EVAL
here: https://redis.io/topics/rediscli#running-lua-scripts
Most helpful comment
--eval
is no 'real' redis command but just an extra option of redis-cli. However it's still possible to do what you want:Keep in mind that you might wanna do some security checks and that
EVAL
requires more options than--eval
. Read more about the difference between--eval
andEVAL
here: https://redis.io/topics/rediscli#running-lua-scripts