【发布时间】:2012-03-30 05:12:36
【问题描述】:
我正在使用Mongoose 来管理 Mongo 数据库。我的连接文件很简单:
var mongoose = require('mongoose')
mongoose.connection.on("open", function(){
console.log("Connection opened to mongodb at %s", config.db.uri)
});
console.log("Connecting to %s", config.db.uri)
mongoose.connect(config.db.uri)
global.mongoose = mongoose
然后在我的 app.js 中我只是
require('./database)
并且“mongoose”变量是全局可用的。我不想使用全局变量(至少不直接使用)。有没有更好的方法通过单例模式或其他方法跨节点共享数据库连接变量(我正在使用 express.js)?
【问题讨论】:
标签: node.js mongodb express mongoose