0

I have a Nodejs app developed using Twitter API. When hosted on Heroku its giving the following error:

2018-12-14T10:00:03.678180+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2018-12-14T10:00:03.678256+00:00 heroku[web.1]: Stopping process with SIGKILL
2018-12-14T10:00:03.739029+00:00 heroku[web.1]: Process exited with status 137

On searching, I came across these StackOverflow question q1 q2 but both of them are using ExpressJS and listening onto a fixed port instead of the one provided by Heroku whereas my app is not using ExpressJS so, I don't understand where the problem lies. I also ran the app locally using heroku local where its working fine. This Heroku article talks about the error but I can't relate it to my app article.
Can anyone explain this error and suggest possible solutions?

My code: code

nourza
  • 2,215
  • 2
  • 16
  • 42
Neeraj Sewani
  • 3,952
  • 6
  • 38
  • 55
  • If the code you've provided is the only code, I don't see you starting a server and listening to a port? – Mavi Domates Dec 14 '18 at 12:03
  • @MaviDomates That is the only code. Do I have to use Express to listen to a port? – Neeraj Sewani Dec 14 '18 at 12:08
  • See my answer. In your local machine the code would execute from beginning to end, but in a web-server you'd have to listen to a port. – Mavi Domates Dec 14 '18 at 12:14
  • @MaviDomates Your solution won't work because the code inside the function passed as the paramter in `createServer` would only run whenever an HTTP request is made to the app whereas in my app no such HTTP request is being made. Instead of this running the app as a worker runs the app perfectly. – Neeraj Sewani Dec 15 '18 at 17:36

2 Answers2

1

If you're not using ExpressJS (which you don't have to - it's just a web-server which makes things easier) you should use the regular http server of Node.

See the code below taken from here - slightly edited.

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.write('Hello World!');
  res.end();
}).listen(process.env.PORT || 8080);
Mavi Domates
  • 4,262
  • 2
  • 26
  • 45
1

Heroku checks an app running as a web app (as defined in the Procfile or by default) for an HTTP request, if it won't find any incoming HTTP request then it would throw the failed to bind to port error. For such apps that are meant to run in the background like this Twitter app, run the app as a worker by defining a worker in the Procfile.
For more info to create the Procfile visit procfile. To know more about web and worker visit link1 link2

Neeraj Sewani
  • 3,952
  • 6
  • 38
  • 55