Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何向Heroku部署一个Node.js应用程序,该应用程序一直发布到端点?_Node.js_Heroku - Fatal编程技术网

如何向Heroku部署一个Node.js应用程序,该应用程序一直发布到端点?

如何向Heroku部署一个Node.js应用程序,该应用程序一直发布到端点?,node.js,heroku,Node.js,Heroku,我创建了一个非常简单的节点应用程序,它有一个单一的函数,每三秒钟就有一个POST端点。它会将它与响应代码一起发布的次数反馈给用户。下面是名为fetch.js的文件中的完整代码: const request = require('request'); let counter = 0 function vote(){ const options = { method: 'POST', url: 'https://voting-vote-producer.r

我创建了一个非常简单的节点应用程序,它有一个单一的函数,每三秒钟就有一个POST端点。它会将它与响应代码一起发布的次数反馈给用户。下面是名为
fetch.js的文件中的完整代码:

const request = require('request');

let counter = 0

function vote(){
    const options = {
        method: 'POST',
        url: 'https://voting-vote-producer.r7.com/vote',
        headers: {
          authority: 'voting-vote-producer.r7.com',
          'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.193 Safari/537.36',
          'content-type': 'application/x-www-form-urlencoded',
          accept: '*/*',
          origin: 'https://afazenda.r7.com',
          'sec-fetch-site': 'same-site',
          'sec-fetch-mode': 'cors',
          'sec-fetch-dest': 'empty',
          referer: 'https://afazenda.r7.com/a-fazenda-12/votacao',
          'accept-language': 'en-US,en;q=0.9,pt;q=0.8'
        },
        form: {voting_id: '268', alternative_id: '648'},
      };
      
      request(options, function (error, response, body) {
        if (error) throw new Error(error);
      
        console.log(response.statusCode);
        console.log(`votos: `, counter)

        counter++
      })
}

setInterval(vote, 3000)

然后,我根据Heroku的规范在
package.json
上创建了一个脚本:

...
"scripts": {
    "start": "node fetch.js"
  },
...

如果我通过
npmstart
node fetch.js
在本地运行,它就会工作。我可以在控制台日志中看到它

但是,在成功部署到Heroku后,根据Heroku日志,我不断收到一个错误:

2020-11-19T15:15:02.738228+00:00 heroku[web.1]: State changed from crashed to starting
2020-11-19T15:15:05.839197+00:00 heroku[web.1]: Starting process with command `npm start`
2020-11-19T15:15:09.505292+00:00 app[web.1]: 
2020-11-19T15:15:09.505330+00:00 app[web.1]: > fazenda@1.0.0 start /app
2020-11-19T15:15:09.505331+00:00 app[web.1]: > node fetch.js
2020-11-19T15:15:09.505331+00:00 app[web.1]: 
2020-11-19T15:16:06.107217+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2020-11-19T15:16:06.130433+00:00 heroku[web.1]: Stopping process with SIGKILL
2020-11-19T15:16:06.251368+00:00 heroku[web.1]: Process exited with status 137
2020-11-19T15:16:06.326134+00:00 heroku[web.1]: State changed from starting to crashed

想法?

您应该使用
worker:node fetch.js
创建。转到Heroku并将dyno formation从
npm start
更改为
node fetch.js

,默认情况下,Heroku需要HTTP服务器才能工作。所以你只需要创建一个HTTP服务器,它什么都不做。您可以使用内置的

例如,将以下内容添加到您的应用程序:

const http = require('http');

const requestListener = function (req, res) {
  res.writeHead(200);
  res.end('Hello, World!');
}

const server = http.createServer(requestListener);
// this should be the last line
server.listen(parseInt(process.env.PORT) || 8080);

请注意,如果Heroku使用
port
环境变量传递所需的端口。

Web进程在启动后60秒内未能绑定到$port,则应用程序没有运行的端口,您需要一台服务器,您不能仅仅部署一个功能,尝试添加一个express服务器,并在该服务器内执行您想要的操作,使用内容
worker:node fetch.js
创建一个
Procfile
。您正在运行一个web进程,它要求您绑定到
$PORT
Procfile
定义一个非web进程的新进程。因此,它不希望绑定到端口。我完全按照您所说的做了,现在我得到了以下信息:
2020-11-19T22:37:36.627501+00:00 heroku[worker.1]:状态从开始更改为2020-11-19T22:37:37.762640+00:00 heroku[worker.1]:使用SIGTERM 2020-11-19T22:37:37.878441+00 heroku[worker.1]停止所有进程:进程退出,状态143 2020-11-19T22:37:39.392500+00:00 heroku[worker.1]:使用命令节点fetch.js 2020-11-19T22:37:40.213464+00:00 heroku[worker.1]启动进程:状态从启动更改为启动
我确实重新启动了dynos,但仍然得到相同的结果error@VellerBauer正如我所看到的,主要问题是“使用SIGTERM停止所有进程“。应用程序进程有30秒时间完全关闭。在此期间,他们应该停止接受新请求,并尝试完成当前请求,或将作业放回队列,以便其他工作进程处理。如果在该时间段后仍有任何进程,dyno manager将使用SIGKILL强制终止这些进程。尝试更改setInterval函数,将秒数设置为更大的数字。我感谢您的解释。不幸的是,即使在将
setInterval
更改为300000之后,我仍然得到SIGKILL。“还有其他想法吗?”维勒鲍尔我在官方和官方网站上找到了类似问题的描述。希望它能有帮助,这是有意义的。我试过了,但仍然得到了这个错误:
2020-11-19T23:03:40.260718+00:00 heroku[web.1]:使用SIGTERM 2020-11-19T23:03:40.343055+00:00 heroku[web.1]停止所有进程:进程退出状态143 2020-11-19T23:03:41.324094+00:00 heroku[web.1]:使用命令节点index.js 2020-11-19T23:03:45.483422+00:00 heroku[web.1]启动进程:状态从启动更改为启动
。我还上传了完整的代码,以防它有助于调试