Getting-started: Setup Database for Node.js

前端 未结 4 957
终归单人心
终归单人心 2021-01-29 17:07

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

相关标签:
4条回答
  • 2021-01-29 17:53

    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!

    http://fzysqr.com/2011/02/28/nodechat-js-using-node-js-backbone-js-socket-io-and-redis-to-make-a-real-time-chat-app/

    0 讨论(0)
  • 2021-01-29 17:58

    In addition to the NodeCamp tutorial you mention there is another NodeCamp tutorial given by Matt Ranney the aforementioned author of the redis node library. It goes into a wee bit more depth.

    0 讨论(0)
  • 2021-01-29 18:01

    Express authentication using Redis for session store and Couchdb for database using coffeescript..

    Check this gist: 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:

    node-mongodb-native by christkv : https://github.com/christkv/node-mongodb-native, or

    mongoose : https://github.com/learnboost/mongoose, or

    amark's mongous: https://github.com/amark/mongous

    0 讨论(0)
  • 2021-01-29 18:04

    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.

    0 讨论(0)
提交回复
热议问题