如何在mongoLab和Heroku服务器中使用mongo URI

我正在使用MongoDb和Nodejs。 Nodejs托pipe在Heroku服务器上,Mongodb通过Heroku插件在MongoLab中。

在安装了MongoLab插件之后,我收到了这个URI:

mongodb://<dbuser>:<dbpassword>@ds053148.mongolab.com:53148/heroku_app18934798 

我不知道如何使用这个URI,请帮忙? 在哪里放? 什么是<dbuser>:<dbpassword

这是我在locallhost中locallhost所关心的configuration

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" } } }; 

       

网上收集的解决方案 "如何在mongoLab和Heroku服务器中使用mongo URI"

您可以使用以下方法连接到数据库:

 var mongoose = require('mongoose'); mongoose.connect('mongodb://<dbuser>:<dbpassword>@ds053148.mongolab.com:53148/<database name>'); 

但在你的情况下,我会用mongodb://<dbuser>:<dbpassword>@ds053148.mongolab.com:53148/<database name>replacemongodb://localhost/<database name> mongodb://<dbuser>:<dbpassword>@ds053148.mongolab.com:53148/<database name>

 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文件中,并用你的用户名replaceyour_user_name ,并用密码replaceyour_password以获得连接。

 mongoURI = 'mongodb://localhost/test'; MONGOLAB_URI = "mongodb://<admin>:<admin>@ds3232.mlab.com:23213/abcd" mongoose.connect(MONGOLAB_URI || mongoURI)