dynamically allocating port in Node JS - node.js

Lately I've been using Node.js to create a chat application and as I went to host it on heroku, it keeps popping up with this error. My code can be found here. I believe it is something to do with the port not being dynamically allocated but how exactly would I do this in this setup?
2015-07-12T11:00:35.931639+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=calm-spire-4645.herokuapp.com request_id=1d5bbae1-6fcc-42cf-b987-d17b45a3ef96 fwd="101.184.151.145" dyno= connect= service= status=503 bytes=

You need to bind the port to the PORT environment variable (this lets the port be set by Heroku).
As you are setting the port in your config file, you could use something like this:
var config = JSON.parse(fs.readFileSync('./config.json'))
var port = process.env.PORT || config.port;
var server = new ws.Server({host: config.host, port: port})

Related

Node, TS, express and Socket.IO server crashes on heroku with code=H10

I've hosted a node TS server on Heroku, and while the application functions perfectly, it crashes when I resume use after some time of inactivity. Below is the error code I see in the heroku logs.
at=error code=H10 desc="App crashed" method=GET path="/socket.io/?EIO=4&transport=polling&t=O838TxJ" host=rgt-server.herokuapp.com request_id=640e7b4e-d679-4ffb-8811-dd369c338004 fwd="102.176.94.160" dyno= connect= service= status=503 bytes= protocol=https
I solve this problem by adding an error handler to my redis client.
// handle redis connection temporarily going down without app crashing
redisClient.on("error", function(err) {
console.error("Error connecting to redis", err);
});

App won't connect to MongoDB using MongoDB code

I can run everything fine locally, and the app shows up on Heroku (all using the local mongoose db), but if I switch to the MongoDB db, using the code provided by MongoDB, both the local (goorm) and the Heroku sites fail when I do anything that needs to address the db.
The first err that appears in the Heroku log is:
2020-08-08T15:57:45.898855+00:00 heroku[web.1]: State changed from starting to crashed
2020-08-08T15:57:46.903138+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=lit-forest-68974.heroku app.com request_id=a99ca76f-f8a8-4790-bf83-d42c24cc17d7 fwd="77.11.27.47" dyno= connect= service= status=503 bytes= protocol=https
2020-08-08T15:57:47.298492+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=lit-forest-68974.herokuapp.com request_id=cf828829-07cf-4c23-861f-3a123039a66f fwd="77.11.27.47" dyno= connect= service= status=503 bytes= protoco
l=https
I am thinking (hoping) that the issue is something in how I reference the db in my app.js file. I am using:
const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://NeilGreer:Ng232117#clearport1.sxp3s.mongodb.net/clearport11?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true , useUnifiedTopology: true});
client.connect(err => {
const collection = client.db("test").collection("devices");
// perform actions on the collection object
client.close();
});
Which was the code directly copied from MongoDB.'
Has anyone run into a similar issue?
You're getting H10 error.
No issues found on your MongoDB connect file as it is working well on local.
Please check below items.
Check whether you have added start script in package.json
"scripts": {
"start": "node index.js"
}
Don't set PORT in your code. Heroku will automatically set PORT and can get from environmental variables.
const PORT = process.env.PORT || 5000
Also check your Procfile whether web dyno commands given correctly.

How to deploy NodeJs, Socket application on Heroku without any error?

I have developed nodejs, socketIO based real time chat application. I have deployed on heroku. Build was successful. After 30-60 seconds, this app will crash every time.
019-11-12T19:39:57.319082+00:00 heroku[router]: at=info method=GET path="/socket.io/?EIO=3&transport=websocket&sid=LjT06AikcmD9YNWMAAAA" host=ichatbd.herokuapp.com request_id=9e763230-a0c9-4dde-a147-f15ca50f7d67 fwd="103.218.24.238" dyno=web.1 connect=0ms service=21766ms status=101 bytes=183 protocol=https
2019-11-12T19:39:57.327674+00:00 heroku[router]: at=info method=GET path="/socket.io/?EIO=3&transport=websocket&sid=_RnJVJZCLbuNcLBsAAAC" host=ichatbd.herokuapp.com request_id=7d6c1d10-9968-4b72-ac90-5eb7405ec9bd fwd="103.218.24.238" dyno=web.1 connect=1ms service=18554ms status=101 bytes=175 protocol=https
2019-11-12T19:39:57.382419+00:00 heroku[web.1]: Process exited with status 1
2019-11-12T19:39:57.333853+00:00 heroku[router]: at=info method=GET path="/socket.io/?EIO=3&transport=websocket&sid=g04ocLYoRLBqkwfTAAAB" host=ichatbd.herokuapp.com request_id=e8f312d2-e337-44ef-88a7-1c255f98a2f4 fwd="175.41.44.170" dyno=web.1 connect=0ms service=21288ms status=101 bytes=175 protocol=https
2019-11-12T19:39:58.398679+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/styles/style.css" host=ichatbd.herokuapp.com request_id=945c9699-1b14-4be0-901e-879dd369e91f fwd="103.218.24.238" dyno=web.1 connect=1ms service= status=503 bytes= protocol=https
2019-11-12T19:39:58.954315+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/socket.io/?EIO=3&transport=polling&t=MvX8Jki" host=ichatbd.herokuapp.com request_id=826c4735-8fd3-4b6f-9f79-f21ef12c0825 fwd="103.218.24.238" dyno= connect= service= status=503 bytes= protocol=https
Cant identify, whats the problem is. After using google, changed on port setup. I have added some code where i just implemented node server.
const express = require('express');
const app = express();
app.set('port', process.env.PORT || 4000);
const server = require('http').Server(app);
const io = require('socket.io')(server);
const port = app.get('port');
So, can anybody help me to identify & solve this problem.
Thanks in advance
you can share us file where you are implementing io.connect.
I believe that you have a bug in somewhere of your code. also you can share us all logs of heroku with heroku cli you can see them eg. heroku logs app xxxxxx(nameapp in heroku)

Why Heroku app crashes following H10 and how to spot the problem?

I am deploying an app to node.js mongodb-Atlas hosted Heroku app and after deploying it, it started to crash. here is the log of the incident:
2019-11-07T14:03:44.54763+00:00 app[web.1]:
2019-11-07T14:03:44.547905+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2019-11-07T14:03:44.548078+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2019-11-07T14_03_43_920Z-debug.log
2019-11-07T14:03:44.609666+00:00 heroku[web.1]: Process exited with status 1
2019-11-07T14:03:44.660116+00:00 heroku[web.1]: State changed from starting to crashed
2019-11-07T14:08:46.771752+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=flix-app-test.herokuapp.com request_id=14978e4f-fb93-44c8-a6c1-294a6a254e1e fwd="84.17.61.226" dyno= connect= service= status=503 bytes= protocol=https
2019-11-07T14:08:47.381666+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=flix-app-test.herokuapp.com request_id=b067c4c5-8b09-44c7-907e-bb2010dd97ec fwd="84.17.61.226" dyno= connect= service= status=503 bytes= protocol=https
2019-11-07T14:15:51.61258+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=flix-app-test.herokuapp.com request_id=5290a4ea-94b1-4d79-a8fe-6709e6cb2a15 fwd="91.168.132.252" dyno= connect= service= status=503 bytes= protocol=https
2019-11-07T14:15:52.254787+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=flix-app-test.herokuapp.com request_id=41f76767-00c8-490c-9c13-22c0906fedb9 fwd="91.168.132.252" dyno= connect= service= status=503 bytes= protocol=https
Most posts I found regarding this H10 error point to an issue on the lines that define the port which through Heroku listens, that it does not accept a rigid one, but the listening of the app is done by this code:
var port = process.env.PORT || 3000;
app.listen(port, "0.0.0.0", function() {
console.log('Listening on Heroku defined port');
});
The code is working locally (double checked), but the Heroku app does not even loads. Databases are running and available and I could find no visible problem.
I'd like to know how should I proceed to find out which part of the code is broken OR to find out that it's a Heroku temporarily issue.
Thanks in advance.
If you are using ExpressJS, just do:
var port = process.env.PORT;
Promise.resolve(app.listen(port)).then(() => {
console.log("Running!");
});
Anyways, 503 is Service Unavailable HTTP code. If the above code does not work, please post your router and controller code for the errored routes.

How to trace app crash in heroku?

My REST api heroku app (using restify) crashes and I dont know why. In heroku logs there is only line:
at=error code=H10 desc="App crashed" method=GET path=/some/api/path host=some.host.com fwd="83.28.44.34" dyno= connect= service= status=503 bytes=
I tried adding this:
process.on('uncaughtException',function(err){
console.log('#########');
console.log(err);
throw err;
});
But it does not writes anything to logs.
The problem is I don't know if app crashes during http request because there are some functions that may be fired even after request is complete...
How I can trace what crashes my app?

Resources