node. js and Redis

Source: Internet
Author: User
Tags redis server

Recently I was learning node to create a project because I was using a structured database like MySQL and wanted to learn something new, so I switched the database to Redis. Redis is a non-relational database. What is the difference between a relational database and a non-relational database? Simply put, there is a concept of the table, one without. The specific difference is Google bar. Here I mainly describe the process of establishing a connection between node. js and Redis, that is, how early node, the Redis ' database is operating inside. So, by default you've installed both of these software.

In the first step, we need to open the Redis server. Open the command line, switch to the Redis installation directory, and enter the command: Redis-server.exe redis.conf


Of course if you have already configured the environment variable, then you can enter Redis-server directly!

Next, we need to download the Redis node client. We can use Redis's client for testing, but if we want to operate in the node program, we have to download node's client. Here we install it via NPM mode.

First, we create a new node code space. Open the command line and switch to the directory to initialize the NPM repository:

NPM Init
At this point you need to follow the prompts to enter the appropriate information on the line.


When you are finished, you will find that the Package.json file appears under the folder. The original operation is to initialize the content inside the Package.json. Now we open the package file and add a three-party dependency inside it:

"Dependencies": {"Redis": "0.7.1"}

Run the command again after saving:

NPM Install

This way we will find a Node_module folder under the directory, with a Redis folder, so that the Redis dependencies are installed!


Next, we need to use Redis's client to implement the operational database. In fact, the Redis client is to give us a series of operations to generate a database of methods, are packaged well, direct call is good.

For example:

var Redis = require (' Redis ') var client = Redis.createclient (); Client.hmset ("Host", "Nation", "China", "home", "Anhui", " Age "," 20 ");
In this way, you can add a piece of data to the database, and we'll lose it if we don't believe it. Plus one end code:

Client.hgetall ("Host", function (err,obj) {
Console.log (obj);});

Output:


Look, so we can get the data we just inserted. If you are familiar with the Redis operating language, we will find that the names of these methods are the same as the Redis operation statements. You do not need to take a callback function without returning a value, and you need to add a callback function to output the data if you need to return the data. (I tried a lot of functions, basically, if there are exceptions, please correct me!) )

Well, the basic process of manipulating the database is that!




node. js and Redis

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.