我是新来的.但是我很高兴try 一下.我使用Express作为web框架,使用Jade作为模板引擎.这两个都很容易得到设置后,从Node Camp this tutorial.

然而,我发现的一个问题是I can't find a simple tutorial for getting a DB set up.我正在try 构建一个基本的聊天应用程序(存储会话和消息).

Does anyone know of a good tutorial?

另外SO post个讨论了dbs的使用——但由于这与我所处的Django/MySQL世界非常不同,我想确保我了解发生了什么.

谢谢

推荐答案

I assume you have 100 installed the correct way using one of these 101(I used the top one).

雷迪斯

我会使用redis作为数据库.首先,它真的是fast,持续的.你需要安装它,但这真的很容易.

make

雷迪斯-cli

接下来你应该自己玩redis.我建议你在Simon Willison分钟前阅读这篇优秀的教程.他和我还建议你只需玩redis-cli就可以了解数据库.

雷迪斯 client

最后,您需要安装一个redis客户端.我建议您使用mranney's node_redis,因为我认为它是开发速度最快、最活跃的客户.

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();
});

在数据库中存储会话

此外,express的作者还创建了一个库,用于使用redis处理您的sessions.

Installation:

npm install connect-redis

Example:

var connect = require('connect')
      , 雷迪斯Store = require('connect-redis');

connect.createServer(
  connect.cookieDecoder(),
  // 5 minutes
  connect.session({ store: new 雷迪斯Store({ maxAge: 300000 }) })
);

在数据库中存储消息

我想我会用sorted set来做这个.使用ZADD存储消息,使用ZRANKZRANGEBYSCORE检索消息.

插座木卫一

最后,如果你想创建一个简单的聊天,我建议你看看socket.伊奥.

插座io的目标是制作实时应用程序

我还使用socket创建了一个聊天室.我在stackoverflow上发布的io.添加持久性+身份验证应该是轻而易举的事.

Node.js相关问答推荐

如何将信号从终端窗口发送到运行在Raspberry Pi上的Puppeteer/Node.js上的webscraper

运行JEST测试时找不到模块&q;错误

node 上的磁盘压力

NodeJS中的Vertex AI GoogleAuthError

当FastifyJS向客户端发送响应时,apache 不会将其发送给他

关于Node.js中的AES加密库的问题

获取驱动器文件夹的直接子文件夹时出现问题

通过 Node js 中的 TBA 执行 netsuite REST upsert 操作出现 401 错误

Express Web 服务器部署到 prod 但 GET 返回超时错误

无法关闭 node.js 中的mongoose 连接

图像存储在后端文件夹中,但使用 multer 和 react.js 在前端找不到

访问 Mongoose 查询之外的变量

将 AllowDiskUse true 添加到 node.js 中的 MongoDB 聚合?

等到文件上传完成的有效方法(mongoose )

使用新react 18.0.0 的 create-react-app my-app redux 错误

从 CoffeeScript 中的数组中删除一个值

从目录 node Js 中检索文件

对不同对象中的函数使用相同的键时,V8 中的函数调用缓慢

Bootstrap 中的 Grunt 依赖冲突

如何让 Mocha 加载定义全局挂钩或实用程序的 helper.js 文件?