To use Redis in node.js, first:
1, there is a Redis installed on the server, of course, installed on the machine is ok
Node.js is installed on this machine, the client
3. The project needs to install nodejs_redis module
Note # 3, instead of installing it locally, install it in your project (reference).
The way to do that is, in a DOS window, go to your project directory and type
npm install redis
This will download a copy of nodejs_redis and place it in the current directory. Look, there’s a new folder: node_modules\redis
\
Write the following code and save it in the current directory \hello.js
\
var redis = require("redis"),/ / call redis
/* Connect to redis database, createClient(port,host,options); CreateClient () = redis.createclient (6379, '127.0.0.1', {}) */
client = redis.createClient(6379.'192.168.159.128'{});// If necessary, validate
//client.auth(password, callback);
// if you'd like to select database 3, instead of 0 (default), call
// client.select(3, function() { /* ... */ });
// Error listening?
client.on("error".function (err) {
console.log("Error " + err);
});
client.set("string key"."string val", redis.print);//set "string key" "string val"
/* redis.print, a callback function that displays the value returned by redis. The result of the previous statement will return "OK" */
client.hset("hash key"."hashtest 1"."some value", redis.print);
client.hset(["hash key"."hashtest 2"."some other value"], redis.print);
// hash key (hash key)
client.hkeys("hash key".function (err, replies) {
console.log(replies.length + " replies:");
replies.forEach(function (reply, i) {
console.log("" + i + ":" + reply);
});
client.hget("hash key"."hashtest 1",redis.print);
End () will not return the value of an element in the "hash key". Quit () will process the statement first and then exit cleanly. / /
//client.end();
client.quit();
});
Copy the code
\
Run:
DOS window, current project directory, enter
node hello.js
\
References:
Github.com/mranney/nod… \
English, but there is no way, here is the most authoritative, the most comprehensive, other places to check, the feeling is bullshit.