Use Heroku addon mongolab with node.js

家住魔仙堡 提交于 2019-12-08 03:43:29

问题


I am using node.js with mongo lab and I have followed the mongolab tutorial (https://devcenter.heroku.com/articles/mongolab#getting-your-connection-uri) to change my url of db like this in my code:

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

I tested with local run, and I indeed see node is trying to connect to this new URL.

But when I deploy to Heroku. Heroku always complain about this. I tried different things for 3 hours got no clue. This is my log, could you please help?

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

回答1:


Your error message is showing that you're trying to connect to your local database on localhost (127.0.0.1).

What you should be doing in your code is using your Herkou environment variables, eg:

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



回答2:


You can connect to your remote mongodb specifying it in the code, and this will affect to the local and cloud implementation, or in heroku, you can use environment variables for cloud use. Go to your app-settings-reveal config vars, and add,
first field: MONGOLAB_URI
second field: mongodb://yourmongolabuser:yourmongolabpass@dsnumber.mongolab.com:number/yourapp

MongoLab shows you the uri, you only must change de User and Password.

Another thing can helps your node app not to crash is changing mode to production, again in env variables: NODE_ENV=production



来源:https://stackoverflow.com/questions/27033357/use-heroku-addon-mongolab-with-node-js

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