Javascript 入门:为 Node.js 设置数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4542694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting-started: Setup Database for Node.js
提问by Emile
I am new to node.js but am excited to try it out. I am using Expressas a web framework, and Jadeas a template engine. Both were easy to get setup following this tutorialfrom Node Camp.
我是 node.js 的新手,但很高兴尝试一下。我使用Express作为 Web 框架,使用Jade作为模板引擎。按照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 posttalks 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.
另一篇 SO 帖子讨论了要使用的 dbs - 但由于这与我所处的 Django/MySQL 世界非常不同,我想确保我了解正在发生的事情。
Thanks!
谢谢!
回答by Alfred
I assume you have npminstalled the correct way using one of these snippets(I used the top one).
我假设您已经使用这些片段之一以正确的方式安装了npm(我使用了最上面的一个)。
Redis
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.
我会使用 redis 作为数据库。一方面,它非常快速、持久。您需要安装它,但这真的很容易。
make
Redis-cli
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。我建议你看看Simon Willison 的这个优秀的教程。他和我还建议你只是玩玩redis-cli
数据库来感受一下。
Redis client
Redis客户端
Finally you need to install a redis client. I would advise you to use mranney's node_redisbecause I think it is the fastest and most actively developed client.
最后你需要安装一个redis客户端。我建议你使用 mranney 的node_redis,因为我认为它是最快和最积极开发的客户端。
Installation
安装
npm install hiredis redis
Simple example, included as example.js:
简单示例,包含在 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 sessionsusing redis.
此外,express 的作者创建了一个库来使用 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 setfor this. Store the messages using ZADD
and retrieve them using ZRANK
, ZRANGEBYSCORE
.
我想我会为此使用排序集。使用存储消息ZADD
,并使用检索它们ZRANK
,ZRANGEBYSCORE
。
Socket.io
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。
socket.io aims to make realtime apps possible in every browser and mobile device, blurring the differences between the different transport mechanisms.
socket.io 旨在使每个浏览器和移动设备中的实时应用程序成为可能,模糊不同传输机制之间的差异。
I also created a chat using socket.io which I posted on stackoverflow. Adding persistence + authentication should be a breeze.
我还使用我在stackoverflow 上发布的 socket.io 创建了一个聊天。添加持久性 + 身份验证应该轻而易举。
回答by Shripad Krishna
Express authentication using Redis for session store and Couchdb for database using coffeescript..
使用 Redis 进行会话存储,使用 Couchdb 进行数据库的快速身份验证。
Check this gist: https://gist.github.com/652819
检查这个要点:https: //gist.github.com/652819
I use this template for most of my projects. You can implement a similar mongodb version of it too using:
我的大部分项目都使用这个模板。您也可以使用以下方法实现类似的 mongodb 版本:
node-mongodb-native by christkv : https://github.com/christkv/node-mongodb-native, or
node-mongodb-native by christkv :https: //github.com/christkv/node-mongodb-native,或
mongoose : https://github.com/learnboost/mongoose, or
猫鼬:https: //github.com/learnboost/mongoose,或
amark's mongous: https://github.com/amark/mongous
amark 的 mongous:https: //github.com/amark/mongous
回答by Cranberry Headache
回答by jslatts
I know this is an old post, but in case anyone else stumbles upon it, I created a tutorial using most of the OP's components, especially the connection to the database. It does have some added complexity with the use of Backbone.js, but it is all in good fun!
我知道这是一篇旧帖子,但以防其他人偶然发现它,我创建了一个使用大部分 OP 组件的教程,尤其是与数据库的连接。使用 Backbone.js 确实增加了一些复杂性,但这一切都很有趣!