App won't connect to MongoDB using MongoDB code - node.js

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.

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);
});

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.

Heroku deploy error

Hi, I have some problems: I tried to deploy a nodejs app on heroku, but when I try to open it, I get this error :
2017-06-08T16:08:43.221350+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=xmpp-discord.herokuapp.com request_id=6692a6c4-2563-474d-90e2-1657d83c7393 fwd="92.157.13.75" dyno= connect= service= status=503 bytes= protocol=https
2017-06-08T16:08:43.433802+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=xmpp-discord.herokuapp.com request_id=ab1f0f7d-4eed-4948-94e2-bb80fcd9887b fwd="92.157.13.75" dyno= connect= service= status=503 bytes= protocol=https
My app is a bridge between Discord and XMPP: here It's github base: Github but I ran it and setuped it, it does work in local but I want to run the program all day even if my PC isn't on.
What should or could I do?
If you need more infos tell me !
Thanks
It's right there in the logs "missing script 'start'". Either in your procfile or due to Heroku defaults your app is trying to launch using "npm start", but your package.json does not define a start script.
Either update your procfile to the correct start command or add to your package.json, in the "scripts" object something like: "start": "node server.js" or whatever command you normally use to start the app.

dynamically allocating port in 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})

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