Configuring Node.js Connection String for Mongo Labs Database on Heroku

只愿长相守 提交于 2019-12-01 11:17:08

问题


My web app (built on Node.js and Express) works fine locally, but when I deploy it to Heroku, I'm unable to connect to my Mongo Labs database. I've changed the connection string in my 'app.js' file to properly reflect the URI of the new Heroku Mongo Labs database (fake username and password substituted below). I've also tried several stackoverlow solutions can't connect to mongolab with node.js on heroku as well as https://devcenter.heroku.com/articles/getting-started-with-nodejs#using-mongodb but those don't seem to work either. I suspect this is slightly different because I'm using Mongoskin.

The original connection:

var db = mongo.db("mongodb://localhost:27017/userdir", {native_parser:true});

changed to the new connection:

var db = mongo.db("mongodb://user:pass@dbh23.mongolab.com:27237/heroku_app24581691",{native_parser:true});

Any ideas as to what I'm missing here?

var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

// Database
var mongo = require('mongoskin');
var db = mongo.db("mongodb://user:pass@dbh23.mongolab.com:27237/heroku_app24581691", {native_parser:true});

// Make db accessible to the router

var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

// Make db accessible to the router
app.use(function(req,res,next){
    req.db = db;
    next();
});

app.use('/', routes);
app.use('/users', users);

/// catch 404 and forwarding to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

/// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});

module.exports = app;

回答1:


I answered this in the comments on my tutorial, but I'll answer it here too: I think it's likely that you need to use the environment variable referenced in the Heroku ducomentation you linked: process.env.MONGOLAB_URI ... my guess is that Heroku doesn't want you putting your username/password directly into the code (and with good reason, since that's not particularly secure). Give it a shot with:

var db = mongo.db(process.env.MONGOLAB_URI, {native_parser:true});

and see how that works out.



来源:https://stackoverflow.com/questions/23326753/configuring-node-js-connection-string-for-mongo-labs-database-on-heroku

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!