使用Heroku插件mongolab和node.js

我正在使用mongo实验室的node.js,我已经按照mongolab教程( https://devcenter.heroku.com/articles/mongolab#getting-your-connection-uri )在我的代码中更改我的数据库的URL :

module.exports = new Db(settings.db, new Server('mongodb://heroku_app3178:e6dd5g3btvanua3gfbmk6@ds051740.mongolab.com', 51740, {}), {safe: true}); 

我testing了本地运行,我确实看到节点正试图连接到这个新的URL。

但是当我部署到Heroku。 Heroku总是抱怨这个。 我尝试了三个小时的不同的事情没有任何线索。 这是我的日志,你能帮忙吗?

 2014-11-20T06:35:55.290572+00:00 heroku[api]: Release v17 created by abcc@gmail.com 2014-11-20T06:35:57.385492+00:00 heroku[web.1]: Starting process with command `node app.js` 2014-11-20T06:35:58.836675+00:00 heroku[web.1]: State changed from starting to up 2014-11-20T06:35:59.876883+00:00 app[web.1]: app.usr local 2014-11-20T06:36:00.396662+00:00 app[web.1]: at Socket.emit (events.js:95:17)a 2014-11-20T06:36:00.394396+00:00 app[web.1]: 2014-11-20T06:36:00.396664+00:00 app[web.1]: at net.js:440:14 2014-11-20T06:36:00.394894+00:00 app[web.1]: throw err; 2014-11-20T06:36:00.396665+00:00 app[web.1]: at process._tickCallback (node.js:419:13) 2014-11-20T06:36:00.396651+00:00 app[web.1]: Error: Error connecting to database: failed to connect to [127.0.0.1:27017] ====> why is this? so confused 2014-11-20T06:36:00.394918+00:00 app[web.1]: ^ 2014-11-20T06:36:00.396654+00:00 app[web.1]: at null.<anonymous> (/app/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/server.js:553:74) 2014-11-20T06:36:00.396656+00:00 app[web.1]: at emit (events.js:106:17) 2014-11-20T06:36:00.396659+00:00 app[web.1]: at emit (events.js:98:17) 2014-11-20T06:36:00.396658+00:00 app[web.1]: at null.<anonymous> (/app/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:140:15) 2014-11-20T06:36:00.396661+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/connection.js:512:10) 2014-11-20T06:36:00.394676+00:00 app[web.1]: /app/node_modules/connect-mongo/lib/connect-mongo.js:178 2014-11-20T06:36:00.415816+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=GET path="/" host=enigmatic-coast-5373.herokuapp.com request_id=165ef7e7-a68a-44b0-9706-f72fcd9a0ba4 fwd="54.83.129.179" dyno=web.1 connect=2ms service=540ms status=503 bytes=0 2014-11-20T06:36:01.075007+00:00 heroku[web.1]: State changed from crashed to starting 2014-11-20T06:36:01.074333+00:00 heroku[web.1]: State changed from up to crashed 2014-11-20T06:36:01.057123+00:00 heroku[web.1]: Process exited with status 8 2014-11-20T06:36:02.976453+00:00 heroku[web.1]: Starting process with command `node app.js` 2014-11-20T06:36:04.328809+00:00 heroku[web.1]: State changed from starting to up 

您的错误消息显示您正尝试连接到本地主机上的本地数据库(127.0.0.1)。

你应该在你的代码中使用你的Herkou环境variables,例如:

 module.exports = new Db(settings.db, new Server(process.env.MONGOLAB_URL, {}), {safe: true}); 

你可以连接到代码中指定的远程mongodb,这会影响到本地和云的实现,或者在heroku中,你可以使用云环境variables来使用。 转到您的应用程序设置,显示configurationvariables,并添加,
第一场:MONGOLAB_URI
第二栏:mongodb:// yourmongolabuser:yourmongolabpass@dsnumber.mongolab.com:number / yourapp

MongoLab显示你的URI,你只需要改变用户名和密码。

另一件事可以帮助你的节点应用程序不崩溃正在改变模式生产,再次在envvariables:NODE_ENV =生产