面对NodeJS在Heroku上的部署错误,我该如何解决?

时间:2020-05-18 00:43:39

标签: node.js api heroku backend

我在Heroku上部署了NodeJS应用,但出现此错误: Image of error

我试图在Heroku上部署NodeJs应用,遵循Heroku指南中的所有步骤,但是我无法使其运行。

我该如何解决?

这是我的 app.js

const express = require('express');
const app = express();
const path = require('path');

app.get('/', async (req, res) => {

  res.sendFile(path.join(__dirname + '/index.html'));
});

app.post('/hello', async (req, res) => {
   // My code ....
});

app.listen(3000, () => console.log('listening on port 3000'));

这是我的 Package.json

{
  "name": "amer-api",
  "version": "1.0.0",
  "description": "Amer's personal website api.",
  "author": "Amer Ansari",
  "license": "ISC",
  "homepage": "https://github.com/AmerAnsari/amer-api",
  "repository": {
    "type": "git",
    "url": "git+https://github.com/AmerAnsari/amer-api.git"
  },
  "bugs": {
    "url": "https://github.com/AmerAnsari/amer-api/issues"
  },
  "scripts": {
    "start": "node app.js"
  },
  "main": "app.js",
  "dependencies": {
    "express": "^4.17.1",
    "geoip-lite": "^1.4.2",
    "nodemailer": "^6.4.6"
  }
}

THere是日志( heroku日志--tail ):

amer@pire:~/Documents/GitHub/amer-api$ heroku logs --tail
2020-05-18T00:31:45.124394+00:00 app[api]: Initial release by user am3ransari@gmail.com
2020-05-18T00:31:45.124394+00:00 app[api]: Release v1 created by user am3ransari@gmail.com
2020-05-18T00:31:45.576958+00:00 app[api]: Enable Logplex by user am3ransari@gmail.com
2020-05-18T00:31:45.576958+00:00 app[api]: Release v2 created by user am3ransari@gmail.com
2020-05-18T00:32:22.000000+00:00 app[api]: Build started by user am3ransari@gmail.com
2020-05-18T00:32:49.104161+00:00 app[api]: Deploy 1d131d7d by user am3ransari@gmail.com
2020-05-18T00:32:49.104161+00:00 app[api]: Release v3 created by user am3ransari@gmail.com
2020-05-18T00:32:49.128088+00:00 app[api]: Scaled to web@1:Free by user am3ransari@gmail.com
2020-05-18T00:32:55.009626+00:00 app[web.1]: 
2020-05-18T00:32:55.009659+00:00 app[web.1]: > amer-api@1.0.0 start /app
2020-05-18T00:32:55.009660+00:00 app[web.1]: > node app.js
2020-05-18T00:32:55.009660+00:00 app[web.1]: 
2020-05-18T00:32:55.349511+00:00 app[web.1]: listening on port 3000
2020-05-18T00:32:58.000000+00:00 app[api]: Build succeeded
2020-05-18T00:33:53.578640+00:00 heroku[web.1]: State changed from starting to crashed
2020-05-18T00:33:53.583026+00:00 heroku[web.1]: State changed from crashed to starting
2020-05-18T00:34:01.570180+00:00 app[web.1]: 
2020-05-18T00:34:01.570202+00:00 app[web.1]: > amer-api@1.0.0 start /app
2020-05-18T00:34:01.570202+00:00 app[web.1]: > node app.js
2020-05-18T00:34:01.570203+00:00 app[web.1]: 
2020-05-18T00:34:02.125267+00:00 app[web.1]: listening on port 3000
2020-05-18T00:34:48.531089+00:00 heroku[router]: at=error code=H20 desc="App boot timeout" method=GET path="/" host=serene-shelf-12092.herokuapp.com request_id=82d5065f-fcd6-4e9c-8836-7ebb3a075658 fwd="92.99.250.143" dyno= connect= service= status=503 bytes= protocol=https
2020-05-18T00:34:59.080489+00:00 heroku[web.1]: State changed from starting to crashed
2020-05-18T00:35:00.097273+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=serene-shelf-12092.herokuapp.com request_id=aa2cac26-b857-48aa-a2eb-72f9c9abea04 fwd="92.99.250.143" dyno= connect= service= status=503 bytes= protocol=https

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

请在您的依赖项中添加路径:

npm install path --save

第二次将您的app.js最后一行更改为此

const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`listening on:`, port));