node.js - 将 Heroku 插件 mongolab 与 Node.js 结合使用

标签 node.js mongodb heroku mlab

我正在将node.js与mongo lab一起使用,并且我按照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});

我使用本地运行进行了测试,我确实看到 Node 正在尝试连接到这个新 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

最佳答案

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

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

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

关于node.js - 将 Heroku 插件 mongolab 与 Node.js 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27033357/

相关文章:

node.js - 在 AWS 上运行的 NodeJS 应用程序的 Mongoose CastError

node.js - Mongoose 过期数据但保留在数据库中

ssl - 免费测功机是否使用 SSL/TLS 连接?

javascript - 遇到EOF时如何不停止读取文件?

node.js - ExpressJS res.render() 错误(JSON.stringify 无法处理循环引用)

javascript - Node.js + Mongoose ...控制流问题?

ruby-on-rails - 如何将请求发送到 http ... 到 https?

docker - Heroku的Docker应用程序在超过40层启动时崩溃

javascript - 使用 JSON 格式的 Node.JS 从 MySQL 中检索数据

javascript - 语法错误: Unexpected token } in JSON at position