开始:安装Node.js数据库 [英] Getting-started: Setup Database for Node.js

查看:261
本文介绍了开始:安装Node.js数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是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).

Redis

I would use redis as a database. For one it is really fast, persistent. You need to install it, but that is really easy.

make

Redis-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 client

Finally 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 redis

Simple 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-redis

Example:

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.io

Finally 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数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆