`
sillycat
  • 浏览: 2551357 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Blog Project(1)Express Backend API - Redis and MongoDB

 
阅读更多
Blog Project(1)Express Backend API - Redis and MongoDB

Move to http://sillycat.ddns.net

NodeJS - Express - MongoDB - Redis - Token Auth - PM2 - Gulp

Token Auth
https://code.tutsplus.com/tutorials/token-based-authentication-with-angularjs-nodejs--cms-22543

mongoDB
https://github.com/alsotang/node-lessons/tree/master/lesson15
http://www.html-js.com/article/Mongoose-based-mongoose-entry-a
https://github.com/airuikun/mongoose_crud
https://segmentfault.com/a/1190000004873740
https://cnodejs.org/topic/504b4924e2b84515770103dd

Set Up and Run MongoDB
http://sillycat.iteye.com/blog/2378280

Create a database and add a user there.
After I checked my mongoDB, the version is too old
>db.version()
2.4.10

Command to connect to the mongo server
>mongo --host sillycat.ddns.net --port 27017 -uxxxx -pxxxxx --authenticationDatabase admin

addUser on old version
https://docs.mongodb.com/v2.4/reference/method/db.addUser/
https://stackoverflow.com/questions/22638258/create-superuser-in-mongo
It seems all other commands are not working on that old version of db.
>db.addUser('sillycat’,'xxxxx');

How to use NodeJS Client
>npm install mongodb —save

>cat app1.js
var MongoClient = require('mongodb').MongoClient;

// Connect to the db
var opts = { db: {authSource:'admin'}};
MongoClient.connect("mongodb://sillycat:xxxxxx@sillycat.ddns.net:27017/mydb", opts, function (err, db) {
     if(err) throw err;
     db.collection('things', function (err, collection) {
         collection.find().toArray(function(err, items) {
            if(err) throw err;   
            console.log(items);           
        });
    });
    db.close();    
});

https://stackoverflow.com/questions/7486623/mongodb-password-with-in-it
>npm install mongoose —save
>cat app2.js
var mongoose = require('mongoose');
mongoose.connect('mongodb://sillycat.ddns.net/mydb?authSource=admin', {user:’xxxxx', pass:’xxxxxx'});

var Thing = mongoose.model('Thing', { name: String });

Thing.find(function(err, items){
if(err) return console.error(err);
console.log(items);
});
mongoose.disconnect();

Set Up and Run Redis
http://sillycat.iteye.com/blog/2378437

Verify the server
>redis-cli -h sillycat.ddns.net -p 6379 -a xxxxx ping
PONG

Verify that from NodeJS
https://redislabs.com/lp/node-js-redis/
https://www.sitepoint.com/using-redis-node-js/
>npm install redis —save
>cat app3.js
var redis = require('redis');
var client = redis.createClient(6379, ‘xxxxx.ddns.net', { no_ready_check:true});
client.auth(‘xxxxxx', function(err){
if(err) throw err;
});

client.set("language","nodejs", redis.print);
client.get("language", function(err, reply){
if(err) throw err;
console.log(reply.toString());
    client.quit();
});


References:
http://www.jianshu.com/p/26def59afaa0


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics