我是node.js的新手,但很高兴试用它。我使用 Express 作为网络框架,并使用 Jade 作为模板引擎。按照本教程从 Node Camp 。
但我遇到的一个问题是我找不到用于获取数据库设置的简单教程。我尝试建立一个基本的聊天应用程序(商店会话和消息)。
有人知道一个好的教程吗?
这其他 SO post 谈论dbs使用,但因为这与我所在的Django / MySQL世界非常不同,我想确保我明白发生了什么。
谢谢!
解决方案我假设您有 npm 使用其中一个程式码片段(我使用前一个)安装正确的方式。
Redis我将使用redis作为数据库。对于一个,它是真的快速,持续。您需要安装它,但这很容易。
make pre> Redis-cli接下来你应该自己玩redis。我建议您查看 Simon Willison 的精彩教程。他和我也建议你只需使用 redis-cli 来获得数据库的感觉。
Redis客户端最后,您需要安装一个redis客户端。我建议您使用mranney的 node_redis ,因为我认为这是最快,最积极开发的客户端。 p>
安装
npm install hiredis redis包含在example.js中的简单示例:
var redis = require(redis), client = redis.createClient client.on(error,function(err){ console.log(Error+ err); }); client.set(string key,string val,redis.print); client.hset(hash key,hashtest 1,some value,redis.print); client.hset([hash key,hashtest 2,some other value],redis.print); client.hkeys(hash key,function(err,replies){ console.log(replies.length +replies:); replies.forEach i){ console.log(+ i +:+ reply); }); client.quit(); }在数据库中存储会话
快速创建者已创建了一个库,以使用redis处理您的会话。
安装:
npm install connect-redis示例:
var connect = require('connect'),RedisStore = require('connect-redis'); connect.createServer( connect.cookieDecoder(), // 5分钟 connect.session({store:new RedisStore({maxAge:300000}) }));在资料库中储存邮件
我将使用排序集。使用 ZADD 存储邮件并使用 ZRANK , ZRANGEBYSCORE 。
Socket.io最后,如果您试图创建一个简单的聊天,我建议您看看socket.io。
socket.io旨在让每个浏览器和移动设备的实时应用程序 $ b设备,模糊不同的运输机制之间的差异。
socket.io,我发布在 stackoverflow 。添加持久性+身份验证应该很轻松。
I am new to node.js but am excited to try it out. I am using Express as a web framework, and Jade as a template engine. Both were easy to get setup following this tutorial from Node Camp.
However the one problem I am finding is I can't find a simple tutorial for getting a DB set up. I am trying to build a basic chat application (store session and message).
Does anyone know of a good tutorial?
This other SO post talks about dbs to use- but as this is very different from the Django/MySQL world I've been in, I want to make sure I understand what is going on.
Thanks!
解决方案I assume you have npm installed the correct way using one of these snippets(I used the top one).
RedisI would use redis as a database. For one it is really fast, persistent. You need to install it, but that is really easy.
makeRedis-cli
Next you should play with redis yourself. I would advice you to look at this excellent tutorial by Simon Willison. He and I also advice you to just play with the redis-cli to get a feeling of the database.
Redis clientFinally you need to install a redis client. I would advise you to use mranney's node_redis because I think it is the fastest and most actively developed client.
Installation
npm install hiredis redisSimple example, included as example.js:
var redis = require("redis"), client = redis.createClient(); client.on("error", function (err) { console.log("Error " + err); }); client.set("string key", "string val", redis.print); client.hset("hash key", "hashtest 1", "some value", redis.print); client.hset(["hash key", "hashtest 2", "some other value"], redis.print); client.hkeys("hash key", function (err, replies) { console.log(replies.length + " replies:"); replies.forEach(function (reply, i) { console.log(" " + i + ": " + reply); }); client.quit(); });Storing sessions in database
Also the author of express has created a library to handle your sessions using redis.
Installation:
npm install connect-redisExample:
var connect = require('connect') , RedisStore = require('connect-redis'); connect.createServer( connect.cookieDecoder(), // 5 minutes connect.session({ store: new RedisStore({ maxAge: 300000 }) }) );Storing messages in database
I think I would use a sorted set for this. Store the messages using ZADD and retrieve them using ZRANK, ZRANGEBYSCORE.
Socket.ioFinally if you are trying to create a simple chat I would advise you to have a look at socket.io.
socket.io aims to make realtime apps possible in every browser and mobile device, blurring the differences between the different transport mechanisms.
I also created a chat using socket.io which I posted on stackoverflow. Adding persistence + authentication should be a breeze.
更多推荐
开始:安装Node.js数据库
发布评论