LoginSignup
19
22

More than 5 years have passed since last update.

Node.jsでRedisと連携する

Posted at

node_redis npmパッケージサイト
https://npmjs.org/package/redis

node_redis GitHubサイト
https://github.com/mranney/node_redis

インストール

$ npm install redis

サンプルコード

app.js
var http = require('http');
var redis = require('redis');
var client = redis.createClient();

var kyeword = '';

//文字列挿入
client.set('mydata', 'こんにちは');

//文字列検索
client.get('mydata',function(err,data){
        keyword = data;
});

//ハッシュデータ検索
var find = ['email', 'score'];
client.hmget('myhash',find,function(err,replies){
    replies.forEach(function(reply, i){
        //コンソールに出力
        console.log(find[i] + ': ' + replies[i]);
    });
});

//ハッシュデータ全リスト
client.hgetall('myhash',function(err,replies){
    //コンソールに出力
    console.log(replies);
});

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});

    //文字列をブラウザに出力
    res.end(keyword + '\n');

}).listen(1337, '127.0.0.1');

console.log('Server running at http://127.0.0.1:1337/');
19
22
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
22