Programing

시작하기 : Node.js 용 데이터베이스 설정

lottogame 2020. 9. 25. 08:13
반응형

시작하기 : Node.js 용 데이터베이스 설정


나는 node.js를 처음 접했지만 그것을 시도하게되어 기쁩니다. 내가 사용하고 Express를 웹 프레임 워크로, 그리고 템플릿 엔진으로. 모두 다음 설치를 쉽게 얻을 수 있었다 이 튜토리얼 에서 노드 캠프 .

그러나 내가 찾은 한 가지 문제 는 DB 설정에 대한 간단한 자습서를 찾을 수 없다는 것 입니다. 기본 채팅 애플리케이션 (스토어 세션 및 메시지)을 구축하려고합니다.

누구든지 좋은 튜토리얼을 알고 있습니까?

이 다른 SO 포스트 는 사용할 dbs에 대해 이야기합니다. 그러나 이것은 제가 있었던 Django / MySQL 세계와 매우 다르기 때문에 무슨 일이 일어나고 있는지 이해하고 싶습니다.

감사!


스 니펫 중 하나를 사용하여 올바른 방법으로 npm을 설치 했다고 가정합니다 (맨 위 스 니펫 사용).

Redis

redis를 데이터베이스로 사용합니다. 하나에게는 정말 빠르고 지속적입니다. 설치해야하지만 정말 쉽습니다.

make

Redis-cli

다음으로 redis로 플레이해야합니다. Simon Willison 의이 훌륭한 튜토리얼을 보도록 조언합니다 . 그와 나는 또한 redis-cli데이터베이스에 대한 느낌을 얻기 위해를 가지고 놀도록 조언 합니다.

Redis 클라이언트

마지막으로 redis 클라이언트를 설치해야합니다. mranney의 node_redis 를 사용 하는 것이 가장 빠르고 가장 활발하게 개발 된 클라이언트라고 생각하기 때문입니다.

설치

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(function (reply, i) {
        console.log("    " + i + ": " + reply);
    });
    client.quit();
});

데이터베이스에 세션 저장

또한 express의 작성자는 redis를 사용 하여 세션 을 처리하는 라이브러리를 만들었습니다 .

설치:

npm install connect-redis

예:

var connect = require('connect')
      , RedisStore = require('connect-redis');

connect.createServer(
  connect.cookieDecoder(),
  // 5 minutes
  connect.session({ store: new RedisStore({ maxAge: 300000 }) })
);

데이터베이스에 메시지 저장

나는 이것을 위해 정렬 된 세트사용할 것이라고 생각 합니다. 사용하여 메시지를 저장 ZADD하고 사용하여 검색 ZRANK, ZRANGEBYSCORE.

Socket.io

마지막으로 간단한 채팅을 생성하려는 경우 socket.io를 살펴 보라고 권합니다.

socket.io는 모든 브라우저와 모바일 장치에서 실시간 앱을 가능하게하여 서로 다른 전송 메커니즘 간의 차이를 모호하게 만드는 것을 목표로합니다.

또한 stackoverflow에 게시 한 socket.io를 사용하여 채팅을 만들었습니다 . 지속성 + 인증을 추가하는 것은 쉽습니다.


세션 저장소에 Redis를 사용하고 coffeescript를 사용하는 데이터베이스에 Couchdb를 사용하는 익스프레스 인증 ..

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


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.


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/

참고URL : https://stackoverflow.com/questions/4542694/getting-started-setup-database-for-node-js

반응형