Node.js Development Access Redis Database tutorial _node.js

Source: Internet
Author: User
Tags redis node redis install redis redis server

Keep in mind that node.js is primarily used to build high-performance, highly scalable server and client applications, and it is geared toward "live Web".
The goal of Node.js is to provide an "easy way to build scalable Web servers", which is impacted by event machines from the Ruby language (events Machine) and the twisted framework from Python.

Redis is an open source, with the use of ANSI C language, support network, can be based on memory can also be persistent log-type, Key-value database, and provide a variety of language APIs. From March 15, 2010 onwards, Redis's development work is hosted by VMware.

1, install the Redis node.js Drive

Copy Code code 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, NPM remote server connection is very slow, how to do?

Consider using NPM's domestic mirror server.

There are three different ways:

1) using config command

Copy Code code 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 designation

Copy Code code as follows:

NPM--registry http://registry.cnpmjs.org Info underscore

3) Edit ~/.NPMRC to add the following:

Copy Code code as follows:

Registry = http://registry.cnpmjs.org

To perform the Redis-driven installation again:

Copy Code code 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
redis@0.10.0 Node_modules/redis

Get!

2. Write test procedure

Copy Code code 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. Execution procedure

Copy Code code as follows:

thinkpad:~/work/redis-node$ node Redis-test.js
Ok
Hello World



Program Description: First connect the remote Redis server, and then write a key/value to Redis, and then read out the key value according to the key name.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.