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

BlogProject(1)ExpressBackendAPI-RedisandMongoDB

NodeJS-Express-MongoDB-Redis-TokenAuth-PM2-Gulp

TokenAuth

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://cnodejs.org/topic/504b4924e2b84515770103dd

SetUpandRunMongoDB

Createadatabaseandaddauserthere.

AfterIcheckedmymongoDB,theversionistooold

>db.version()

2.4.10

Commandtoconnecttothemongoserver

>mongo--hostsillycat.ddns.net--port27017-uxxxx-pxxxxx--authenticationDatabaseadmin

addUseronoldversion

https://docs.mongodb.com/v2.4/reference/method/db.addUser/

https://stackoverflow.com/questions/22638258/create-superuser-in-mongo

Itseemsallothercommandsarenotworkingonthatoldversionofdb.

>db.addUser('sillycat’,'xxxxx');

HowtouseNodeJSClient

>npminstallmongodb—save

>catapp1.js

varMongoClient=require('mongodb').MongoClient;

//Connecttothedb

varopts={db:{authSource:'admin'}};

MongoClient.connect("mongodb://sillycat:xxxxxx@sillycat.ddns.net:27017/mydb",opts,function(err,db){

if(err)throwerr;

db.collection('things',function(err,collection){

collection.find().toArray(function(err,items){

if(err)throwerr;

console.log(items);

});

});

db.close();

});

https://stackoverflow.com/questions/7486623/mongodb-password-with-in-it

>npminstallmongoose—save

>catapp2.js

varmongoose=require('mongoose');

mongoose.connect('mongodb://sillycat.ddns.net/mydb?authSource=admin',{user:’xxxxx',pass:’xxxxxx'});

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

Thing.find(function(err,items){

if(err)returnconsole.error(err);

console.log(items);

});

mongoose.disconnect();

SetUpandRunRedis

Verifytheserver

>redis-cli-hsillycat.ddns.net-p6379-axxxxxping

PONG

VerifythatfromNodeJS

https://redislabs.com/lp/node-js-redis/

https://www.sitepoint.com/using-redis-node-js/

>npminstallredis—save

>catapp3.js

varredis=require('redis');

varclient=redis.createClient(6379,‘xxxxx.ddns.net',{no_ready_check:true});

client.auth(‘xxxxxx',function(err){

if(err)throwerr;

});

client.set("language","nodejs",redis.print);

client.get("language",function(err,reply){

if(err)throwerr;

console.log(reply.toString());

client.quit();

});

References:

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

相关推荐