我正在使用MongoDb和Nodejs。 Nodejs托管在Heroku服务器上,Mongodb通过Heroku附加组件在MongoLab中托管。
安装了MongoLab插件后,我收到了这个URI:
mongodb://<dbuser>:<dbpassword>@ds053148.mongolab.com:53148/heroku_app18934798
我不知道如何使用这个URI,请帮帮忙?
在哪里放?什么是<dbuser>:<dbpassword
这是我在locallhost
在server.js
var env = process.env.NODE_ENV || 'development',
config = require('./config/config')[env],
mongoose = require('mongoose');
var db = mongoose.connect(config.db);
在express.js
app.use(express.session({
secret: 'thissecret',
key:'express.sid',
store: new mongoStore({
url: config.db,
collection: 'sessions'
})
}));
在config.js
module.exports = {
development: {
db: 'mongodb://localhost/mean-dev',
root: rootPath,
app: {
name: 'MEAN - A Modern Stack - Development'
},
facebook: {
clientID: "APP_ID",
clientSecret: "APP_SECRET",
callbackURL: "http://localhost:3000/auth/facebook/callback"
}
},
test: {
db: 'mongodb://localhost/mean-test',
root: rootPath,
app: {
name: 'MEAN - A Modern Stack - Test'
},
facebook: {
clientID: "APP_ID",
clientSecret: "APP_SECRET",
callbackURL: "http://localhost:3000/auth/facebook/callback"
}
},
production: {
db: 'mongodb://localhost/mean',
root: rootPath,
app: {
name: 'MEAN - A Modern Stack - Production'
},
facebook: {
clientID: "APP_ID",
clientSecret: "APP_SECRET",
callbackURL: "http://localhost:3000/auth/facebook/callback"
}
}
};
答案 0 :(得分:3)
您可以使用以下方法连接到数据库:
var mongoose = require('mongoose');
mongoose.connect('mongodb://<dbuser>:<dbpassword>@ds053148.mongolab.com:53148/<database name>');
但在您的情况下,我会将mongodb://localhost/<database name>
替换为mongodb://<dbuser>:<dbpassword>@ds053148.mongolab.com:53148/<database name>
答案 1 :(得分:1)
mongoURI = 'mongodb://localhost/test';
MONGOLAB_URI = "mongodb://<admin>:<admin>@ds3232.mlab.com:23213/abcd"
mongoose.connect(MONGOLAB_URI || mongoURI)
答案 2 :(得分:0)
module.exports = {
development: {
db: 'mongodb://your_user_name:your_password@ds053148.mongolab.com:53148/heroku_app18934798',
root: rootPath,
app: {
name: 'MEAN - A Modern Stack - Development'
},
facebook: {
clientID: "APP_ID",
clientSecret: "APP_SECRET",
callbackURL: "http://localhost:3000/auth/facebook/callback"
}
},
test: {
db: 'mongodb://your_user_name:your_password@ds053148.mongolab.com:53148/heroku_app18934798',
root: rootPath,
app: {
name: 'MEAN - A Modern Stack - Test'
},
facebook: {
clientID: "APP_ID",
clientSecret: "APP_SECRET",
callbackURL: "http://localhost:3000/auth/facebook/callback"
}
},
production: {
db: 'mongodb://your_user_name:your_password@ds053148.mongolab.com:53148/heroku_app18934798',
root: rootPath,
app: {
name: 'MEAN - A Modern Stack - Production'
},
facebook: {
clientID: "APP_ID",
clientSecret: "APP_SECRET",
callbackURL: "http://localhost:3000/auth/facebook/callback"
}
}
};
将其粘贴到您的config.js文件中,并将your_user_name
替换为您的用户名,并将your_password
替换为您的密码以获取连接。