5

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

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

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

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

这是我在 mongoDB 中关心的配置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"
        }

    }
};
4

3 回答 3

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>

于 2013-10-31T02:33:14.563 回答
1
mongoURI = 'mongodb://localhost/test';

MONGOLAB_URI = "mongodb://<admin>:<admin>@ds3232.mlab.com:23213/abcd"

mongoose.connect(MONGOLAB_URI || mongoURI)
于 2017-06-01T15:50:36.730 回答
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为您的密码以获取连接。

于 2013-10-31T06:18:16.553 回答