Heroku -dyno停止运行/休眠,在其中添加'var port = process.env.PORT || 3000'

问题描述 投票:1回答:1

我是一名Swift开发人员,而不是本机Node.js开发人员,因此我不熟悉此问题。

我注册了Heroku,创建了一个应用,免费使用了dyno,在450小时后,我收到一封电子邮件,说Your app(s) have stopped running and will not restart until you receive more free dyno hours next month.

我添加了我的信用卡进行验证,并获得了additional free 550 hrs。我立即恢复到1000小时的免费小时,但是一旦我这样做,dyno就再也无法运行了。

[刚刚(一周后),我跑了

heroku ps -a myAppName

它说我有1000小时。

Free dyno hours quota remaining this month: 1000h 0m (100%)
Free dyno usage for this app: 0h 0m (0%)

我刚刚运行了heroku ps:restart --app myAppName,然后大约15分钟后又回来了,它仍然说我使用了0h 0m (0%)

我运行了heroku logs --app myAppName,并收到以下错误:

错误R10(引导超时)-> Web进程未能绑定到$ PORT启动60秒

SIGKILL进程的停止进程已退出,状态为137 State

从开始更改为崩溃

Heroku says更改端口并添加:

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
    console.log(`Our app is running on port ${ PORT }`);
});

假设这是测功机停止工作/睡眠的原因,看来(我对此不熟悉)我需要将端口代码添加到我的app.js文件中吗? 我不知道要添加使其正常工作的模块。我要添加什么才能使上述端口代码正常工作?例如。上面的代码中的app是什么?

我的procfile

worker: node ./app.js

我的app.js文件

const dotenv = require('dotenv');
dotenv.config();

const admin = require("firebase-admin");
admin.initializeApp({
  credential: admin.credential.cert({
    projectId: process.env.FIREBASE_PROJECT_ID,
    clientEmail: process.env.FIREBASE_CLIENT_EMAIL,
    privateKey: process.env.FIREBASE_PRIVATE_KEY.replace(/\\n/g, '\n'),
  }),
  databaseURL: process.env.FIREBASE_DATABASE_URL
});

const firebase = require('firebase');
const config = {
    apiKey: process.env.FIREBASE_API_KEY,
    databaseURL: process.env.FIREBASE_DATABASE_URL,
    storageBucket: process.env.FIREBASE_STORAGE_BUCKET,GCM_SENDER_ID
};
firebase.initializeApp(config);

const algoliasearch = require('algoliasearch');
// rest of code ...

我的package.json文件:

{
  "name": "my project",
  "version": "1.0.0",
  "description": "Config Files",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node app.js"
  },
  "author": "Lance Samaria",
  "license": "ISC",
  "engines": {
    "node": "10.16.0"
  },
  "dependencies": {
    "algoliasearch": "^3.35.1",
    "dotenv": "^8.2.0",
    "firebase": "^7.2.1",
    "firebase-admin": "^8.6.1",
    "nodejs-latest": "^1.1.0"
  }
}
node.js heroku
1个回答
1
投票

我确实在herehere周围进行了搜索,发现我必须安装express模块。我使用了this answer

$ npm install --save express

然后在我添加的app.js文件的顶部:

var express = require('express');
var app = express();

const PORT = process.env.PORT || 5000;

app.listen(PORT, () => {
  console.log(`Server is listening on port ${PORT}`);
});

一旦我安装了上面的代码,测功机将再次开始工作。我用5000代替了3000]中的here

© www.soinside.com 2019 - 2024. All rights reserved.