Everyone should remember that Node.js is mainly used to build high-performance, highly scalable server and client applications, and it is aimed at "real-time web".
The goal of Node.js is to provide a "built a scalable web server in a simple way" that is influenced by event machines from the Ruby language and Twisted framework from Python.
Redis is an open source, written in ANSI C language, supports network, memory-based and persistent log-type, Key-Value database, and provides APIs in multiple languages. From March 15, 2010, Redis development work was chaired by VMware.
1. Install the Node.js driver for Redis
The code copy is as follows:
ThinkPad:~/work$ mkdir redis-node
ThinkPad:~/work$ cd redis-node
ThinkPad:~/work/redis-node$ ls
ThinkPad:~/work/redis-node$ npm install redis
npm http GET https://registry.npmjs.org/redis
The computer is stuck here and the npm remote server connection is very slow. What should I do?
Consider using NPM's domestic mirror server.
There are three ways:
1) Use the config command
The code copy is as follows:
npm config set registry http://registry.cnpmjs.org
npm info underscore (If the above configuration is correct, this command will have a string response)
2) Command line specification
The code copy is as follows:
npm --registry http://registry.cnpmjs.org info underscore
3) Edit ~/.npmrc to add the following content:
The code copy is as follows:
registry = http://registry.cnpmjs.org
Redis driver installation again:
The code copy is as follows:
ThinkPad:~/work/redis-node$ npm install redis
npm http GET http://registry.cnpmjs.org/redis
npm http 304 http://registry.cnpmjs.org/redis
[email protected] node_modules/redis
Get it done!
2. Write a test program
The code copy is as follows:
// redis-test.js
var redis = require("redis"),
client = redis.createClient(6379, "10.3.30.186");
client.on("error", function(err){
console.log("Error: " + err);
});
client.on("connect", function(){
// start server();
client.set("name_key", "hello world", function(err, reply){
console.log(reply.toString());
});
client.get("name_key", function(err, reply){
console.log(reply.toString());
});
})
3. Execute the program
The code copy is as follows:
ThinkPad:~/work/redis-node$ node redis-test.js
OK
hello world
Program description: First connect to the remote Redis server, then write a key/value to Redis, and then read the key value according to the key name.