Node-redis: executing redis eval command to run Lua script in nodeJS

Created on 22 Jun 2017  ·  1Comment  ·  Source: NodeRedis/node-redis

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
question

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:

'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

>All comments

--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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

id0Sch picture id0Sch  ·  4Comments

gpascale picture gpascale  ·  4Comments

dotSlashLu picture dotSlashLu  ·  5Comments

abhaygarg picture abhaygarg  ·  5Comments

Mickael-van-der-Beek picture Mickael-van-der-Beek  ·  6Comments