Starting my Node.js + Mongo API with Heroku - node.js

I know it's maybe a beginner question, but I looked over it everywhere and I still having trouble with it.
I have a Node API that I run locally. The only thing I need to do is run in terminal:
$ mongod
and then
$ node src/loader.js
So, I upload my code to Heroku and added to my package.json:
"scripts": {
"start": "node src/loader.js"
}
But when I run '$ heroku ps' it says that my process crashed. Whats the difference between my local build and the Heroku build?
Adding my Heroku logs --tail.
2018-02-05T19:19:17.648257+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2018-02-05T19:19:17.648257+00:00 heroku[web.1]: Stopping process with SIGKILL
2018-02-05T19:19:17.732544+00:00 heroku[web.1]: Process exited with status 137

Start by having a look over the heroku logs from terminal 'heroku logs --tail', and if you need more detail you can go to the app in heroku and in the upper right view the logs in the heroku platform.
If you can post some of the logs it will be helpful in telling what's going wrong if you can't derive it from there on your review.

So, I discovered the problem. Heroku dynamically assigns your app a port, so you can't set the port to a fixed number, and in my code I assigned to port: '3000'.
So you need to change to
var port = process.env.PORT || 3000;

Related

Deploying a node.js app (discord.jd bot) on Heroku causing an error

I'm trying to deploy a discord bot on heroku. It works for about 1-2 minutes and then there is an error:
2020-08-10T10:05:17.228802+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2020-08-10T10:05:17.246146+00:00 heroku[web.1]: Stopping process with SIGKILL
2020-08-10T10:05:17.307500+00:00 heroku[web.1]: Process exited with status 137
2020-08-10T10:05:17.347445+00:00 heroku[web.1]: State changed from starting to crashed
(complete log):
2020-08-10T10:04:19.372794+00:00 app[web.1]:
2020-08-10T10:04:19.372804+00:00 app[web.1]: > arepee-bot#1.0.0 start /app
2020-08-10T10:04:19.372804+00:00 app[web.1]: > node index.js
2020-08-10T10:04:19.372804+00:00 app[web.1]:
2020-08-10T10:04:20.468443+00:00 app[web.1]: Bot [welcomeMessage.js] is ready!
2020-08-10T10:04:30.643838+00:00 app[web.1]: Bot [index.js] is ready!
2020-08-10T10:05:17.228802+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2020-08-10T10:05:17.246146+00:00 heroku[web.1]: Stopping process with SIGKILL
2020-08-10T10:05:17.307500+00:00 heroku[web.1]: Process exited with status 137
2020-08-10T10:05:17.347445+00:00 heroku[web.1]: State changed from starting to crashed
the start command in the package.json is: node index.js
the Procfile is:
worker: node index.js
and index.js is:
const Discord = require("discord.js")
const client = new Discord.Client()
const welcomeMessage = require("./welcomeMessage")
client.on("ready", () => {
console.log("Bot [index.js] is ready!")
})
client.login(process.env.TOKEN)
I also have another file, welcomeMessage:
const Discord = require("discord.js")
const client = new Discord.Client()
client.on("ready", () => {
console.log("Bot [welcomeMessage.js] is ready!")
})
client.on('guildMemberAdd', member => {
let welcomeChannel = client.channels.cache.get('0000000000000')
welcomeChannel.send("hello world")
})
client.login(process.env.TOKEN)
What causes this peoblem? thx
Man, somehow I did here, first time ever a random try worked for me, I'm so happy! Look, I hope it will work for you too. First of all, go see your "package.json" file, inside it you must have the:
"scripts": {
},
You should put the "start" : "node index.js" there (even if there's already another things inside 'scripts', but don't delete the other stuff, just add this one) it will be like that:
"scripts": {
"start": "node index.js",
},
Then go to your Procfile and change the "worker : node index.js" to "worker npm start" (I tried it because I notice only web was working and the command of it was "web npm start")
It should solve the problem!
I'm pretty sure that it was what solved for me, now it's running non-stop, but if it's not what solved, I also used some commands on the Terminal, the commands: "heroku run worker", also "heroku ps:scale worker=1".
I hope it will help you, I was 8 hours straight trying to make it work because I had other errors, and I know how annoying is this thing not working.
Obs.: also you need to do what the other person said before:
"(...) you need to disable the web dyno and enable the worker dyno".
PS.: I know you already did the start thing, I'm just thinking if someone get here without this information maybe it will help.
Turns out that you need to disable the web dyno and enable the worker dyno.

"Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch" on node.js deploy with Docker in Heroku

I'm trying to deploy an app to Heroku which was dockernized.
This is my Dockerfile:
FROM node:4.2.2
WORKDIR /usr/src
RUN git clone https://github.com/***.git
WORKDIR /usr/src/application
RUN ./install.sh
EXPOSE 80 3000
CMD bash -C '/usr/src/application/start.sh'
Also, I have a Profile with just one line as follow:
web: node bin/www
Following the steps on the documentation, I've pushed the image via two commands:
heroku container:push web --app immense-falls-39679 and released with
heroku container:release web --app immense-falls-39679.
In the logs, everything seems to be going ok, with the app deployed successfully. But when it is about to start (with the start.sh script) it crashes with the error
2018-10-23T20:47:44.025502+00:00 app[web.1]: [20:47:44] Finished 'build-app' after 7.11 s
2018-10-23T20:47:44.025958+00:00 app[web.1]: [20:47:44] Starting 'bundle-release'...
2018-10-23T20:48:07.932228+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2018-10-23T20:48:07.932327+00:00 heroku[web.1]: Stopping process with SIGKILL
2018-10-23T20:48:08.058850+00:00 heroku[web.1]: Process exited with status 137
2018-10-23T20:48:08.097770+00:00 heroku[web.1]: State changed from starting to crashed
I've researched and most have said that this is due to fixed setting the PORT value. But, in my bin/www (where is my server configs), I've already set the port to be either a fixed value or from the process.env
var app = require('../out/app');
var debug = require('debug')('server:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
app.set('port', (process.env.PORT || 3000));
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(app.get('port'), function() {
listenForNotificationRequests();
});
server.on('error', onError);
server.on('listening', onListening);
I dont have any other clues on how fixing it. I would really appreciate any help. Thanks in advance
To deploy a docker container to Heroku you'll need to adjust your Dockerfile to work with Heroku.
Remove EXPOSE 80 3000, because it is not used by Heroku
When you deploy, the app must listen on $PORT set by Heroku. Your app can access this port with process.env.PORT. If you'd rather pass the port in the Dockerfile cmd then you can use CMD start_app_command -p $PORT
run heroku logs --tail after deploying and you will see that $PORT is replaced with the port set by Heroku.
https://devcenter.heroku.com/articles/container-registry-and-runtime

Heroku Node app keeps crashing

I have an Express app hosted on Heroku. Locally it runs just fine, but keeps crashing on Heroku. Every time I go to it I get the 'Application Error' page.
I double checked all environment variables and they seem ok.
The Heroku logs don't exactly offer much help:
2017-04-16T23:02:49.001768+00:00 heroku[web.1]: State changed from crashed
to starting
2017-04-16T23:02:52.072906+00:00 heroku[web.1]: Starting process with
command `npm run start`
2017-04-16T23:02:56.042611+00:00 app[web.1]:
2017-04-16T23:02:56.042628+00:00 app[web.1]: > my-app#0.0.1 start /app
2017-04-16T23:02:56.042629+00:00 app[web.1]: > node bin/app
2017-04-16T23:02:56.042630+00:00 app[web.1]:
2017-04-16T23:02:57.200912+00:00 heroku[web.1]: State changed from starting
to crashed
2017-04-16T23:02:57.201454+00:00 heroku[web.1]: State changed from crashed
to starting
2017-04-16T23:02:57.192198+00:00 heroku[web.1]: Process exited with status 0
2017-04-16T23:03:00.981528+00:00 heroku[web.1]: Starting process with
command `npm run start`
2017-04-16T23:03:04.312475+00:00 app[web.1]:
2017-04-16T23:03:04.312489+00:00 app[web.1]: > my-app#0.0.1 start /app
2017-04-16T23:03:04.312490+00:00 app[web.1]: > node bin/app
2017-04-16T23:03:04.312491+00:00 app[web.1]:
2017-04-16T23:03:06.093211+00:00 heroku[web.1]: Process exited with status 0
2017-04-16T23:03:06.112842+00:00 heroku[web.1]: State changed from starting
to crashed
So then I tried starting the app manually:
$ heroku run bash
...
$ npm run start
I got no errors. I'm using the pm2 process manager, so I checked it to see if the app is running and it seems like it is:
$ node_modules/.bin/pm2 list
...
But then when I go to open my app I get the same 'Application error' page as before.
'heroku restart' doesn't help. It just crashes again every time.
The only difference between my local environment and Heroku (as far as I can tell at least...) is that locally I have a file from which I'm reading a RSA private key, and on Heroku that key is read from the environment.
Edit: As requested, here's my Procfile:
web: npm run start
Any ideas?
Your app need to start listening to the port defined in the env. For example if you have an expressjs app it should look something like this:
const express = require('express')
const app = express()
const port = process.env.PORT || 3000
app.get('/', (req, res) => res.send("Hello world"))
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
If the app does not bind to the correct port, heroku is going to consider the app unhealthy and will terminate it.
You need to see this line in the logs, which indicates that heroku recognized the app as healthy:
State changed from starting to up
Keep in mind that this port thing is a special requirement for the web process. The other processes in your app does not have such a requirement.
Try 2 things:
Change your procfile:
web: node start.js
Then make sure that your web process it running by scaling it up:
heroku ps:scale web=1
On your computer in a terminal window run this command in your project:
heroku local web
It simulates how Heroku runs your app and should give you information of what is happening.

Running an IRC bot on Heroku

I've got my bot up and running, and I would like it to run on Heroku to keep it persistently connected to our IRC-channel. This is the content of my procfile:
web: coffee marvin.coffee
(The bot's name is marvin).
And this is marvin.coffee
irc = require 'irc'
config = require('./config').config
client = new irc.Client(config.server, config.nick, config.options)
# IRC-listeners
I'm never creating any HTTP server, and I was hoping that it was possible to simply run this coffeescript on Heroku in order for my bot to run, but I get the following error:
Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
The bot is launched, and joins the channel, but immediately leaves due to the fact that Heroku kills the process due to this error.
Any help would be greatly appreciated :-)
Does it work if you change web: coffee marvin.coffee to bot: coffee marvin.coffee in your Procfile?

Node.js port issue on Heroku cedar stack

I'm running a basic Express app in Node.js and trying to deploy to Heroku. The app works fine locally and I believe my setup with Heroku has gone well up until starting the server where i get the following error:
2011-09-21T16:42:36+00:00 heroku[web.1]: State changed from created to starting
2011-09-21T16:42:39+00:00 app[web.1]: Express server listening on port 3000 in production mode
2011-09-21T16:42:40+00:00 heroku[web.1]: Error R11 (Bad bind) -> Process bound to port 3000, should be 12810 (see environment variable PORT)
2011-09-21T16:42:40+00:00 heroku[web.1]: Stopping process with SIGKILL
2011-09-21T16:42:40+00:00 heroku[web.1]: Process exited
this is currently all i have in my app.js
app.listen(3000);
I did also run this as mentioned on Heroku's getting started.
$ heroku config:add NODE_ENV=production
Adding config vars:
NODE_ENV => production
I believe I just need to set up the port for production? Thanks.
Can you show the entire section of code where you call listen? You should be checking for the process environment variable PORT, not just hardcoding it to 3000. From their docs:
var port = process.env.PORT || 3000;
app.listen(port, function() {

Resources