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

本文介绍如何使用Node.js连接并操作MongoDB与Redis数据库,包括认证配置、基本CRUD操作等,并提供示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值