将Heroku addon mongolab与node.js一起使用

时间:2014-11-20 06:42:53

标签: node.js mongodb heroku mlab

我在mongo lab中使用node.js,我已经按照mongolab教程(https://devcenter.heroku.com/articles/mongolab#getting-your-connection-uri)在我的代码中更改了我的db url:

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

我使用本地运行测试,我确实看到节点正在尝试连接到这个新URL。

但是当我部署到Heroku时。 Heroku总是抱怨这个。 我尝试了3个小时不同的事情一点都不清楚。 这是我的日志,你能帮忙吗?

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

2 个答案:

答案 0 :(得分:2)

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

您在代码中应该使用的是Herkou环境变量,例如:

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

答案 1 :(得分:0)

您可以连接到在代码中指定它的远程mongodb,这将影响本地和云实现,或者在heroku中,您可以使用环境变量进行云使用。转到您的app-settings-reveal配置变量,然后添加,
第一栏:MONGOLAB_URI
第二栏:mongodb:// yourmongolabuser:yourmongolabpass@dsnumber.mongolab.com:number / yourapp

MongoLab向您显示uri,您只需要更改用户名和密码。

另一件事可以帮助您的节点应用程序不崩溃是将模式更改为生产,再次在env变量中: NODE_ENV =生产