Node.js开发之访问Redis数据库教程
大家要记住,Node.js主要用于构建高性能、高可伸缩性的服务器和客户端应用,它面向的是“实时Web”。
Node.js的目标是提供一个“以简单的方式构建可扩展的网络服务器”,它受到来自Ruby语言的事件机(EventMachine)和来自Python的Twisted框架的影响。
Redis是一个开源的使用ANSIC语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。从2010年3月15日起,Redis的开发工作由VMware主持。
1、安装Redis的Node.js驱动
ThinkPad:~/work$mkdirredis-node ThinkPad:~/work$cdredis-node ThinkPad:~/work/redis-node$ls ThinkPad:~/work/redis-node$npminstallredis npmhttpGEThttps://registry.npmjs.org/redis
计算机卡在了这里,npm远程服务器连接十分缓慢,怎么办?
考虑使用NPM的国内镜像服务器。
有三种方法:
1)使用config命令
npmconfigsetregistryhttp://registry.cnpmjs.org npminfounderscore(如果上面配置正确这个命令会有字符串response)
2)命令行指定
npm--registryhttp://registry.cnpmjs.orginfounderscore
3)编辑~/.npmrc加入以下内容:
registry=http://registry.cnpmjs.org
再次执行Redis驱动的安装:
ThinkPad:~/work/redis-node$npminstallredis npmhttpGEThttp://registry.cnpmjs.org/redis npmhttp304http://registry.cnpmjs.org/redis redis@0.10.0node_modules/redis
搞定!
2、编写测试程序
//redis-test.js
varredis=require("redis"),
client=redis.createClient(6379,"10.3.30.186");
client.on("error",function(err){ console.log("Error:"+err); });
client.on("connect",function(){ //startserver(); client.set("name_key","helloworld",function(err,reply){ console.log(reply.toString()); });
client.get("name_key",function(err,reply){ console.log(reply.toString()); }); })
热门推荐
2 成都训练狗狗
3 怎么训练小狗狗
4 训练狗狗软件
5 如何训练狗狗拉屎
9 狗狗负重训练
10 狗狗多大可以训练大小便
11 怎么训练狗狗萨摩耶
12 狗狗训练书
13 鞍山狗狗训练
14 训练狗狗用品
15 怎样才能训练狗狗听话
16 狗狗如何训练大小便
17 怎样训练狗狗睡狗窝
18 训练狗狗工具