简体   繁体   English

我正在尝试部署 Heroku,但出现此错误 - code = H10, desc = app crashed method=GET path="/" status=503

[英]I am trying to deploy Heroku and I am getting this error - code = H10, desc = app crashed method=GET path="/" status=503

I am trying to deploy Heroku and I am getting the following error.我正在尝试部署 Heroku,但出现以下错误。 I have added the right script in the package.json as well and have web: node app.js in the Procfile as well.我也在 package.json 中添加了正确的脚本,并且在 Procfile 中也有web: node app.js I am not sure what is going wrong.我不确定出了什么问题。 Any help will be appreciated.任何帮助将不胜感激。

2020-11-13T00:08:36.927175+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-11-13T00:08:36.937119+00:00 app[web.1]: 
2020-11-13T00:08:36.937376+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-11-13T00:08:36.937493+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2020-11-13T00_08_36_928Z-debug.log
2020-11-13T00:08:37.022916+00:00 heroku[web.1]: Process exited with status 1
2020-11-13T00:08:37.099623+00:00 heroku[web.1]: State changed from starting to crashed
2020-11-13T00:08:37.103557+00:00 heroku[web.1]: State changed from crashed to starting
2020-11-13T00:08:40.893537+00:00 heroku[web.1]: Starting process with command `npm start`
2020-11-13T00:08:46.434145+00:00 app[web.1]: 
2020-11-13T00:08:46.434159+00:00 app[web.1]: > NotesAppGIT@1.0.0 start /app
2020-11-13T00:08:46.434159+00:00 app[web.1]: > node app.js
2020-11-13T00:08:46.434160+00:00 app[web.1]: 
2020-11-13T00:09:17.069358+00:00 app[web.1]: Unable to connect to database
2020-11-13T00:09:17.091792+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-11-13T00:09:17.092287+00:00 app[web.1]: npm ERR! errno 1
2020-11-13T00:09:17.111324+00:00 app[web.1]: npm ERR! NotesAppGIT@1.0.0 start: `node app.js`
2020-11-13T00:09:17.111442+00:00 app[web.1]: npm ERR! Exit status 1
2020-11-13T00:09:17.111634+00:00 app[web.1]: npm ERR!
2020-11-13T00:09:17.111801+00:00 app[web.1]: npm ERR! Failed at the NotesAppGIT@1.0.0 start script.
2020-11-13T00:09:17.111949+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-11-13T00:09:17.121499+00:00 app[web.1]: 
2020-11-13T00:09:17.131690+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-11-13T00:09:17.131692+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2020-11-13T00_09_17_112Z-debug.log
2020-11-13T00:09:17.214878+00:00 heroku[web.1]: Process exited with status 1
2020-11-13T00:09:17.254440+00:00 heroku[web.1]: State changed from starting to crashed
2020-11-13T00:09:18.344634+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=notes-app-tanmay.herokuapp.com request_id=d05a7b5f-6e9b-46a5-9c32-65a8850ca8d8 fwd="81.173.75.97" dyno= connect= service= status=503 bytes= protocol=https
2020-11-13T00:09:18.406623+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=notes-app-tanmay.herokuapp.com request_id=5313647a-7b67-498d-a6b3-609a8a99e33c fwd="81.173.75.97" dyno= connect= service= status=503 bytes= protocol=https
2020-11-13T00:10:54.105165+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/getNotes" host=notes-app-tanmay.herokuapp.com request_id=5249c422-c8d0-4024-b15a-bb7d46cd0eb9 fwd="81.173.75.97" dyno= connect= service= status=503 bytes= protocol=https
2020-11-13T00:20:05.000000+00:00 app[api]: Build started by user t.kulkarni@student.fontys.nl
2020-11-13T00:20:14.000000+00:00 app[api]: Build failed -- check your build output: https://dashboard.heroku.com/apps/234008c5-0108-4501-8638-a536a6f392bc/activity/builds/68ca6bf4-5b07-4d64-b751-29189c2271cc
2020-11-13T00:21:56.000000+00:00 app[api]: Build started by user t.kulkarni@student.fontys.nl
2020-11-13T00:22:21.550005+00:00 app[api]: Deploy f854e773 by user t.kulkarni@student.fontys.nl
2020-11-13T00:22:21.550005+00:00 app[api]: Release v7 created by user t.kulkarni@student.fontys.nl
2020-11-13T00:22:22.544958+00:00 heroku[web.1]: State changed from crashed to starting
2020-11-13T00:22:26.000000+00:00 app[api]: Build succeeded
2020-11-13T00:22:26.340735+00:00 heroku[web.1]: Starting process with command `node app.js`
2020-11-13T00:22:58.765046+00:00 app[web.1]: Unable to connect to database
2020-11-13T00:22:58.831915+00:00 heroku[web.1]: Process exited with status 1
2020-11-13T00:22:58.903673+00:00 heroku[web.1]: State changed from starting to crashed
2020-11-13T00:22:58.907147+00:00 heroku[web.1]: State changed from crashed to starting
2020-11-13T00:23:02.186041+00:00 heroku[web.1]: Starting process with command `node app.js`
2020-11-13T00:23:34.617141+00:00 app[web.1]: Unable to connect to database
2020-11-13T00:23:34.674900+00:00 heroku[web.1]: Process exited with status 1
2020-11-13T00:23:34.746652+00:00 heroku[web.1]: State changed from starting to crashed
2020-11-13T00:23:35.188323+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=notes-app-tanmay.herokuapp.com request_id=616fd65b-5fd6-492e-b221-e1131b9cbff8 fwd="81.173.75.97" dyno= connect= service= status=503 bytes= protocol=https
2020-11-13T00:23:38.591573+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/getNotes" host=notes-app-tanmay.herokuapp.com request_id=c708048e-b554-4f1f-856b-dc27f8f9318f fwd="81.173.75.97" dyno= connect= service= status=503 bytes= protocol=https

The following is my db.js file以下是我的 db.js 文件

// Import MongoDB Drivers

const MongoClient = require("mongodb").MongoClient; // Initialize mongodb to required MongoClient
const ObjectID = require('mongodb').ObjectID; // Initialize mongodb to required ObjectID
const dbname = "NotesAppGIT"; // State the DB name as "crud_mongodb"
const url = "mongodb://localhost:27017"; // Specify the location of mongoDB on local
const mongoOptions = {useNewUrlParser : true, useUnifiedTopology: true}; // Set flags for NewUrlParser & UnifiedTopology as true and pass them as MongoOptions

// Create connection between NodeJS & MongoDB Server

// State the initial default state as null
const state =
    {
        db : null
    };

// Connect Method

const connect = (cb) => // pass call back
{
    if(state.db) // If there is a database connection, call the callback function
    {
        cb();
    }
    else // If there is no database connection use the MongoClient to connect to the database
    {
        MongoClient.connect(url,mongoOptions,(err,client)=> // Use MongoClient to connect to the database
        {
            if(err) // If there are errors pass it through the call back function
            {
                cb(err);
            }
            else // If there are no errors set the state of the database and call the call back function
            {
                state.db = client.db(dbname);
                cb();
            }
        });
    }
}

// Primary Key Function

const getPrimaryKey = (_id) => // pass the id of the document
{
    return ObjectID(_id); // Return the ObjectID along with it's ID. This will be used to query the database by ID
}

// Get DB Method

const getDB = ()=> // return the state of the database
{
    return state.db;
}

module.exports = {getDB, connect, getPrimaryKey}; // Pass all the functions though module.exports

The following is my app.js file以下是我的 app.js 文件

// Import modules as variables

const express = require('express'); // Required for routing
const bodyParser = require("body-parser"); // Required to pass JSON data between Client Side & Server Side
const app = express(); // Create instance of express application
app.use(bodyParser.json()); // State the express application to use the body-parser module and parse JSON data through it
const path = require('path'); // Required to serve a static HTML file

const db = require("./db"); // Initialize db as required db path
const collection = "notes"; // Initialize name of the collections as "notes"

// GET Routes

// Send a static HTML file to the user

app.get('/', (req, res) => // Root get path & pass response & request object
{
    res.sendFile(path.join(__dirname, 'index.html')); // Send a static HTML file from the path module
});

// Query the database for all the notes from the notes collection and return them to the user

app.get('/getNotes', (req, res) => // getNotes path & pass response & request object
{
    db.getDB().collection(collection).find({}).toArray((err, documents) => // Call the getDB method (Line #51 od db.js file) & pass the name of the collection.Call the method find to have all the documents from the Notes collection.
    {
        if (err) // If there is an error, print an error in the console for the user
        {
            console.log(err);
        } else // If there is no error, print the documents returned form the server and get the response of the documents in JSON format
        {
            res.json(documents);
        }
    });
});


// UPDATE Routes

// Pass a route parameter. The id is the primary key of the document needed to update. Pass the request & response function

app.put('/:id', (req, res) => {
    const notesID = req.params.id; // get the ID of the notes
    const userInput = req.body; // get the input of the notes from the user. User's data will be in the form of JSON

    // Connect to the database and called the notes collection. Call a function findOneAndUpdate and pass the first argument as query object hence pass find by ID & notes. Pass the document for update, Pass the options and set returnOriginal to false. Pass the call back and pass error and result

    db.getDB().collection(collection).findOneAndUpdate({_id: db.getPrimaryKey(notesID)}, {$set: {notes: userInput.notes}}, {returnOriginal: false}, (err, result) => {
        if (err) // If there is an error, print an error in the console for the user
        {
            console.log(err);
        } else // If there is no error then display the result as a response in JSON format
        {
            res.json(result);
        }
    });
});

// CREATE Route

app.post('/', (req, res) => {
    const userInput = req.body;
    db.getDB().collection(collection).insertOne(userInput, (err, result) => {
        if (err)
            console.log(err);
        else {
            res.json({result: result, document: result.ops[0]});
        }
    });
});


// DELETE Route

app.delete('/:id', (req, res) => {
    const notesID = req.params.id;

    db.getDB().collection(collection).findOneAndDelete({_id: db.getPrimaryKey(notesID)}, (err, result) => {
        if (err) {
            console.log(err);
        } else {
            res.json(result);
        }
    });
});

// Connect to the database [line #19 of db.js file]

db.connect((err) => // Pass a callback
{
    if (err) // If there is an error in connecting to the database print "Unable to connect to database" in the console and terminate the application
    {
        console.log('Unable to connect to database');
        process.exit(1);
    } else // If a database connection is successful, listen on port 3000 and print "connected to database, app listening on port 3000" in the console
    {
        app.listen(process.env.PORT || 3000, () => {
            console.log("Express server listening on port %d in %s mode", this.address().port, app.settings.env);
        });
    }
})

The following is my package.json file以下是我的 package.json 文件

{
  "name": "NotesAppGIT",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "start": "node app.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "express": "^4.17.1",
    "mongodb": "^3.6.2",
    "node": "^15.0.1",
    "path": "^0.12.7"
  }
}

I am not sure why this is happening.我不确定为什么会这样。 Any help will be very much appreciated.任何帮助将不胜感激。 Thank you谢谢

Screenshot of the error -错误截图 -

在此处输入图片说明

It seems like there is an issue with the Database URL . Database URL似乎存在问题。 Try replacing it with the Production Database URL.尝试将其替换为生产数据库 URL。

db.js file db.js 文件

const url = "mongodb://localhost:27017"; // change this to production URL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Heroku 无法启动我的 app.js(代码 H10 状态 503) - Heroku Couldn't start my app.js (Code H10 status 503) Heroku Node.js 错误代码 H10“应用程序崩溃” - Heroku Node.js error code H10 "App crashed" 修复 Heroku 以状态 1 退出,错误代码 H10 - Fixing Heroku exited with status 1, error code H10 将 JS 应用程序部署到 Heroku - 错误 H10 503 错误,应用程序崩溃 - Deploying JS app to Heroku - Error H10 503 error, app crashes Heroku Node.js 应用程序“进程以状态 1 退出”和错误 h10 - Heroku Node.js app “Process exited with status 1” and error h10 第一次将 Express 后端部署到 Heroku 2 错误代码 h10 进程以状态 1 退出 - First time deploying an express backend to Heroku 2 error code h10 Process exited with status 1 我正在尝试使用 heroku 部署一个简单的 nodejs 应用程序,它已成功部署,但在打开 url 时出错? - I am trying to deploy a simple nodejs app using heroku and its is deployed successfully but giving error on opening the url? 应用程序前端的 Heroku 应用程序崩溃 h10 错误 - Heroku app crash h10 error on app's frontend 部署 Heroku 时应用程序崩溃(代码 h10) - App Crashing when Deploying Heroku (Code h10) 在本地运行的 React 应用程序在 Heroku 上崩溃:H10“应用程序崩溃” - React app that runs locally crashes on Heroku: H10 "App crashed"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM