【问题标题】:Heroku won't run mongolab appHeroku 不会运行 mongolab 应用程序
【发布时间】:2018-09-14 16:37:15
【问题描述】:

我正在遵循来自 this freecodecamp post 的确切说明(除了我没有存储和使用连接 URI .env 变量),但应用程序的网页显示“应用程序中发生错误,您的页面可能不送达。如果您是应用程序所有者,请查看您的日志以了解详细信息。'

以下是我的 app.js 中的内容,我非常肯定用户名和密码正确且格式正确。

//lets require/import the mongodb native drivers.
var mongodb = require('mongodb');

//We need to work with "MongoClient" interface in order to connect to a mongodb server.
var MongoClient = mongodb.MongoClient;

// Connection URL. This is where your mongodb server is running.

//(Focus on This Variable)
var url = 'mongodb://user1:123456@ds139959.mlab.com:39959/url-shortener';
//'mongodb://user1:123456@ds139959.mlab.com:39959/url-shortener';     
//(Focus on This Variable)

// Use connect method to connect to the Server
  MongoClient.connect(url, function (err, db) {
  if (err) {
    console.log('Unable to connect to the mongoDB server. Error:', err);
  } else {
    console.log('Connection established to', url);

    // do some work here with the database.

    //Close connection
    db.close();
  }
});

这是我输入“heroku 日志”后的日志消息

2017-06-26T13:11:15.395121+00:00 heroku[路由器]: at=error code=H10 desc="应用程序崩溃" 方法=GET 路径="/" 主机=herokudatabaseprovisioning.herokuapp.com request_id=f2f3d85e-154c-4169-8c8c-9a1f4bdee05c fwd="137.132.242.118" dyno=连接=服务=状态=503字节=协议=https

2017-06-26T13:11:16.634793+00:00 heroku[路由器]: at=error code=H10 desc="应用程序崩溃" 方法=GET path="/favicon.ico" 主机=herokudatabaseprovisioning.herokuapp.com request_id=01d79a9d-0088-4dac-b582-79e08c8e0858 fwd="137.132.242.118 dyno=连接=服务=状态=503字节=协议=https

此外,我从 heroku 日志中仔细检查了一些解决此错误类型的堆栈溢出答案,但所有这些都是由使用 node.js 的本机服务器 .listen() 方法引起的,没有一个解决问题的方法使用mongodb的MongoClient.connect()method

如何解决这个问题?严重的是,我已经被困在这两个星期了。 这是github中全部源代码的the repo

【问题讨论】:

    标签: heroku mlab


    【解决方案1】:

    我刚刚浏览了您的 package.json 。您没有指定您的脚本参数。这是一个修改后的版本。还要确保将您的 procfile 重命名为 Procfile。

    {
        "name": "trial",
        "version": "1.0.0",
        "description": "Test for Hesington",
        "scripts": {
            "start": "node app.js"
        },
        "main": "app.js",
        "dependencies": {
            "mongodb": "^2.2.29"
        },
        "devDependencies": {},
        "scripts": {
            "test": "echo \"Error: no test specified\" && exit 1"
        },
        "author": "",
        "engines": {
        "node": "6.10.3",
            "npm": "5.0.2"
        },
        "license": "ISC",
        "repository": {
            "type": "git",
            "url": "git+https://github.com/hesingon/FFCdbprovision.git"
        },
        "bugs": {
            "url": "https://github.com/hesingon/FFCdbprovision/issues"
        },
        "homepage": "https://github.com/hesingon/FFCdbprovision#readme"
    }
    

    【讨论】:

    • 哦,我明白了。我刚刚将文件更改为web: node app.js,但它似乎仍然无法正常工作:/
    • 您的 package.json 文件也有问题。我已经更正了它们并编辑了我以前的帖子。
    • 非常感谢,我已经设法让它从命令heroku logs --tail 输出预期的日志消息,在这种情况下是2017-06-27T02:38:58.857556+00:00 app[web.1]: Connection established to mongodb://user1:123456@ds139959.mlab.com:39959/url-shortener。但是,当我使用命令heroku open 打开应用程序页面时,我仍然看到显示应用程序错误。这是正常的,因为我没有提供任何 html 文件吗?
    • 是的,这很正常,您也没有在 db.close 之前进行任何操作,因此应用程序在干净退出时终止。
    猜你喜欢
    • 2016-03-30
    • 2012-08-17
    • 2016-05-16
    • 2012-12-07
    • 2014-11-03
    • 2014-11-20
    • 2013-04-24
    • 1970-01-01
    • 2021-05-14
    相关资源
    最近更新 更多