怎么用 mongoose 链接BAE的数据库
贴出部分代码
config.js
var config = {
username:"4qakk7pmG5tkyl1mne6eS04n",
password:"Oek6K2RpwPUoi0a2Obk0sAPDV0tBLUyi",
db_host:"mongo.duapp.com",
db_name:"zJIdwZIQgNwTSKddFmqM",
db_port:"8908"
}
var localhost = {
username:"",
password:"",
db_host:"localhost",
db_name:"express-mongoose-demo",
db_port:"27011"
}
module.exports = config;
db.js
var mongoose = require('mongoose');
var config = require('../config');
var db = exports.Db = mongoose.createConnection();
var options = {
db: { native_parser: true },
server: { poolSize: 5 },
user: config.username,
pass: config.password
}
db.open(config.db_host, config.db_name, config.db_port, options);
//监听BAE mongodb异常后关闭闲置连接
db.on('error', function (err) {
db.close();
});
//监听db close event并重新连接
db.on('close', function () {
db.open(config.db_host, config.db_name, config.db_port, options);
});
怎么调用
10 回复