Can't connect to MongoDB using environment variable for Heroku - node.js

My app works great when I hardcode the Mongo database connection, but when I try to use an environment variable, I get the errors in the log dump below.
I set the environment variable as follows:
$ heroku config:set MONGODB_URI='mongodb+srv://user:password#cluster0-ua7mc.mongodb.net/local_library?retryWrites=true'
(Note: I tried multiple iterations with and without quotes, and made sure the username and password were correct, etc.)
Here is the log dump:
2019-04-26T23:54:22.761573+00:00 heroku[web.1]: Restarting
2019-04-26T23:54:22.768606+00:00 heroku[web.1]: State changed from up to starting
2019-04-26T23:54:22.587447+00:00 app[api]: Release v14 created by user user#hotmail.com
2019-04-26T23:54:22.587447+00:00 app[api]: Set MONGODB_URI config vars by user user#hotmail.com
2019-04-26T23:54:23.508862+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2019-04-26T23:54:23.588782+00:00 heroku[web.1]: Process exited with status 143
2019-04-26T23:54:26.341850+00:00 heroku[web.1]: Starting process with command `npm start`
2019-04-26T23:54:28.837322+00:00 app[web.1]:
2019-04-26T23:54:28.837353+00:00 app[web.1]: > express-locallibrary-tutorial#0.0.0 start /app
2019-04-26T23:54:28.837356+00:00 app[web.1]: > node ./bin/www
2019-04-26T23:54:28.837357+00:00 app[web.1]:
2019-04-26T23:54:30.638883+00:00 heroku[web.1]: State changed from starting to up
2019-04-26T23:54:30.921094+00:00 app[web.1]: MongoDB Connection Error { MongoNetworkError: connection 5 to cluster0-shard-00-02-ua7mc.mongodb.net:27017 closed
2019-04-26T23:54:30.921142+00:00 app[web.1]: at TLSSocket.<anonymous> (/app/node_modules/mongodb-core/lib/connection/connection.js:352:9)
2019-04-26T23:54:30.921145+00:00 app[web.1]: at Object.onceWrapper (events.js:273:13)
2019-04-26T23:54:30.921146+00:00 app[web.1]: at TLSSocket.emit (events.js:182:13)
2019-04-26T23:54:30.921148+00:00 app[web.1]: at _handle.close (net.js:610:12)
2019-04-26T23:54:30.921150+00:00 app[web.1]: at TCP.done (_tls_wrap.js:386:7)
2019-04-26T23:54:30.921151+00:00 app[web.1]: name: 'MongoNetworkError',
2019-04-26T23:54:30.921153+00:00 app[web.1]: errorLabels: [ 'TransientTransactionError' ],
2019-04-26T23:54:30.921155+00:00 app[web.1]: [Symbol(mongoErrorContextSymbol)]: {} }
2019-04-26T23:55:26.350787+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path="/catalog" host=immense-bucket-10578.herokuapp.com request_id=65fc54a9-5195-463b-a31a-f0ece0248800 fwd="47.136.227.118" dyno=web.1 connect=0ms service=30001ms status=503 bytes=0 protocol=https
2019-04-26T23:55:26.352964+00:00 app[web.1]: GET /catalog - - ms - -
Code dealing with Mongo Connection and environment variable is here:
var mongoose = require('mongoose');
var dev_db_url = 'mongodb+srv://user:password#cluster0-r92qb.mongodb.net/local_library?retryWrites=true';
var mongoDB = process.env.MONGODB_URI || dev_db_url;
mongoose.connect(mongoDB, {useNewUrlParser: true});
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
Many thanks in advance if any ideas!
SOLUTION: Changing Whitelist IP to 0.0.0.0/0 fixed the problem.

Try to set the variable via the dashboard. If still not connect then the problem should be with your mongo URI.

Related

How can I set a timeout function to prevent Heroku from returning H13 error code?

I have a tiny backend with literally 1 endpoint. It works flawlessly on my local machine. After a cold startup, it takes roughly 4 seconds to return on my local machine. But when I deploy to Heroku, it boots up and loads fine, but if you try to utilize it, it crashes with code H13 which after some research is equivalent to Heroku intentionally crashing it because it took too long to load. From what I can tell it's supposed to happen after 30 seconds of attempting to load, though after timing it, it's closer to about 10 before it crashes. I've also seen 1 potential workaround where they used a timeout function every 3 seconds to check the status of the server, though frankly the code was beyond me. My best guess is that the await OpenseaScraper is taking too long to respond, but a single call to an api shouldn't be that long right?
Here is my code
const express = require('express');
const OpenseaScraper = require('opensea-scraper')
const mongoose = require('mongoose');
require('dotenv').config()
const NFT = require('./NFTModel')
const server = express()
const router = express.Router()
mongoose.connect(process.env.MONGODB_URL, () => console.log('This application has been connected to the Mogo DB!'))
router.get('/', async (req, res) => {
const ranking = await OpenseaScraper.rankings()
let NFTs = []
try {
ranking.forEach(async item => {
const slug = item.slug
const name = item.name
const logoUrl = item.logo
let floorPrice = 0
if (item.floorPrice == null) {
floorPrice = 0
} else {
floorPrice = item.floorPrice.amount
}
NFTs.push({name, slug, logoUrl, floorPrice})
NFT.findOne({ "slug": slug }, (err, value) => {
if (err) { console.error(err) }
if (!value) {
let newNFT = new NFT({slug, name, logoUrl, floorPrice})
newNFT.save() }
})
})
} catch(err) {
console.log(err)
res.status(400).end()
}
res.json(NFTs)
})
server.use(router)
server.listen(process.env.PORT || 5000, () => {
console.log(`Express is working on port ${process.env.PORT}`);
});
Here is the [crash]
[1]: https://i.stack.imgur.com/8GvD4.png
2022-06-12T02:52:42.150870+00:00 heroku[web.1]: State changed from starting to up
2022-06-12T02:52:42.485695+00:00 app[web.1]: This application has been connected to the Mogo DB!
2022-06-12T02:53:19.480795+00:00 app[web.1]: /app/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:241
2022-06-12T02:53:19.480803+00:00 app[web.1]: reject(new Error([
2022-06-12T02:53:19.480803+00:00 app[web.1]: ^
2022-06-12T02:53:19.480803+00:00 app[web.1]:
2022-06-12T02:53:19.480804+00:00 app[web.1]: Error: Failed to launch the browser process!
2022-06-12T02:53:19.480818+00:00 app[web.1]: /app/node_modules/puppeteer/.local-chromium/linux-982053/chrome-linux/chrome: error while loading shared libraries: libnss3.so: cannot open shared object file: No such file or directory
2022-06-12T02:53:19.480818+00:00 app[web.1]:
2022-06-12T02:53:19.480818+00:00 app[web.1]:
2022-06-12T02:53:19.480819+00:00 app[web.1]: TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md
2022-06-12T02:53:19.480819+00:00 app[web.1]:
2022-06-12T02:53:19.480819+00:00 app[web.1]: at onClose (/app/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:241:20)
2022-06-12T02:53:19.480820+00:00 app[web.1]: at Interface.<anonymous> (/app/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:231:68)
2022-06-12T02:53:19.480820+00:00 app[web.1]: at Interface.emit (node:events:539:35)
2022-06-12T02:53:19.480820+00:00 app[web.1]: at Interface.close (node:readline:586:8)
2022-06-12T02:53:19.480821+00:00 app[web.1]: at Socket.onend (node:readline:277:10)
2022-06-12T02:53:19.480822+00:00 app[web.1]: at Socket.emit (node:events:539:35)
2022-06-12T02:53:19.480822+00:00 app[web.1]: at endReadableNT (node:internal/streams/readable:1345:12)
2022-06-12T02:53:19.480822+00:00 app[web.1]: at processTicksAndRejections (node:internal/process/task_queues:83:21)
2022-06-12T02:53:19.485994+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=GET path="/" host=nft-backend-open-sea.herokuapp.com request_id=22e86188-d5ab-4e1a-a123-64ade9c345ab fwd="216.198.98.78" dyno=web.1 connect=0ms service=2510ms status=503 bytes=0 protocol=https
2022-06-12T02:53:19.594772+00:00 heroku[web.1]: Process exited with status 1
2022-06-12T02:53:19.717227+00:00 heroku[web.1]: State changed from up to crashed

Connection Terminated Connecting to Private Space Postgres from Inside Private Space

I'm attempting to display rows of data from a private space Postgres instance attached to app-a from another app - app-b, inside the same Private Space.
Running heroku local didn't work - which I thought was due to my personal IP not being whitelisted, so I pushed to app-b. I've also added the credential for app-b - to no avail. app-b has no DB of it's own and I've tried adding credential while DATABASE_URL is manually filled, and with no DATABASE_URL on abb-b - none of which worked.
I'm able to run heroku pg:psql from app-a but not from app-b, leading me to think the credential may not be attached/incorrectly attached?
My code is simple and shown below:
var { Client } = require('pg');
app.get('/db', (req, res) => {
var dbOpts = {
connectionString: process.env.DATABASE_URL,
ssl : {
rejectUnauthorized: false
}
}
const client = new Client(dbOpts);
client.connect();
client.query('SELECT FirstName, Id FROM salesforce.user', (err, dbRes) => {
console.log('inside');
if (err) {
console.log('DB Route err: ', err);
throw err;
}
console.log('dbRes: ', dbRes);
res.render('pages/db',{
results : dbRes.rows
});
});
client.end();
});
As soon as I hit /db the following error is thrown
2022-03-04T08:21:30.057536+00:00 app[web.1]: /app/index.js:25
2022-03-04T08:21:30.057537+00:00 app[web.1]: throw err;
2022-03-04T08:21:30.057537+00:00 app[web.1]: ^
2022-03-04T08:21:30.057537+00:00 app[web.1]:
2022-03-04T08:21:30.057537+00:00 app[web.1]: Error: Connection terminated
2022-03-04T08:21:30.057538+00:00 app[web.1]: at Connection.<anonymous> (/app/node_modules/pg/lib/client.js:132:36)
2022-03-04T08:21:30.057538+00:00 app[web.1]: at Object.onceWrapper (node:events:641:28)
2022-03-04T08:21:30.057538+00:00 app[web.1]: at Connection.emit (node:events:539:35)
2022-03-04T08:21:30.057539+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/pg/lib/connection.js:57:12)
2022-03-04T08:21:30.057539+00:00 app[web.1]: at Socket.emit (node:events:527:28)
2022-03-04T08:21:30.057539+00:00 app[web.1]: at TCP.<anonymous> (node:net:688:12)
2022-03-04T08:21:30.057540+00:00 app[web.1]:
2022-03-04T08:21:30.057540+00:00 app[web.1]: Node.js v17.6.0
2022-03-04T08:21:30.055860+00:00 app[web.1]: inside
2022-03-04T08:21:30.057190+00:00 app[web.1]: DB Route err: Error: Connection terminated
2022-03-04T08:21:30.057191+00:00 app[web.1]: at Connection.<anonymous> (/app/node_modules/pg/lib/client.js:132:36)
2022-03-04T08:21:30.057192+00:00 app[web.1]: at Object.onceWrapper (node:events:641:28)
2022-03-04T08:21:30.057192+00:00 app[web.1]: at Connection.emit (node:events:539:35)
2022-03-04T08:21:30.057192+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/pg/lib/connection.js:57:12)
2022-03-04T08:21:30.057193+00:00 app[web.1]: at Socket.emit (node:events:527:28)
2022-03-04T08:21:30.057193+00:00 app[web.1]: at TCP.<anonymous> (node:net:688:12)
2022-03-04T08:21:30.060340+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=GET path="/db" host=agdc-hconnect-view.herokuapp.com request_id=3e850bf1-c88a-68bc-6f21-0ef82b960ce9 fwd="75.111.73.234" dyno=web.1 connect=0ms service=19ms status=503 bytes=561 protocol=http tls_version=tls1.3
2022-03-04T08:21:30.659072+00:00 heroku[web.1]: Process exited with status 1
2022-03-04T08:21:30.679550+00:00 heroku[web.1]: State changed from up to crashed

Trying to connect to Heroku: MongoParseError: URI malformedm cannot be parsed

I'm trying to post my app to Heroku for class and it keeps crashing. This is the error code I'm receiving. I'm not exactly sure what's happening, the H10 error is a blanket statement, maybe someone here could shed some light. This is what I'm getting when using heroku logs --tail.
Edit: My app works totally fine on my local machine, I'm only getting this error when trying to post to Heroku
2022-02-13T18:27:21.000000+00:00 app[api]: Build started by user nmendez6594#gmail.com
2022-02-13T18:27:38.841499+00:00 app[api]: Release v5 created by user nmendez6594#gmail.com
2022-02-13T18:27:38.841499+00:00 app[api]: Deploy 7d112609 by user nmendez6594#gmail.com
2022-02-13T18:27:39.000000+00:00 app[api]: Build succeeded
2022-02-13T18:27:39.104174+00:00 heroku[web.1]: State changed from crashed to starting
2022-02-13T18:27:40.901836+00:00 heroku[web.1]: Starting process with command `npm start`
2022-02-13T18:27:42.058731+00:00 app[web.1]:
2022-02-13T18:27:42.058745+00:00 app[web.1]: > budget-app#1.0.0 start
2022-02-13T18:27:42.058746+00:00 app[web.1]: > node server.js
2022-02-13T18:27:42.058746+00:00 app[web.1]:
2022-02-13T18:27:42.395832+00:00 app[web.1]: App running on port 55680!
2022-02-13T18:27:42.401378+00:00 app[web.1]: /app/node_modules/mongodb/lib/core/uri_parser.js:580
2022-02-13T18:27:42.401379+00:00 app[web.1]: return callback(new MongoParseError('URI malformed, cannot be parsed'));
2022-02-13T18:27:42.401380+00:00 app[web.1]: ^
2022-02-13T18:27:42.401380+00:00 app[web.1]:
2022-02-13T18:27:42.401380+00:00 app[web.1]: MongoParseError: URI malformed, cannot be parsed
2022-02-13T18:27:42.401381+00:00 app[web.1]: at parseConnectionString (/app/node_modules/mongodb/lib/core/uri_parser.js:580:21)
2022-02-13T18:27:42.401381+00:00 app[web.1]: at connect (/app/node_modules/mongodb/lib/operations/connect.js:283:3)
2022-02-13T18:27:42.401382+00:00 app[web.1]: at /app/node_modules/mongodb/lib/mongo_client.js:284:5
2022-02-13T18:27:42.401382+00:00 app[web.1]: at maybePromise (/app/node_modules/mongodb/lib/utils.js:692:3)
2022-02-13T18:27:42.401382+00:00 app[web.1]: at MongoClient.connect (/app/node_modules/mongodb/lib/mongo_client.js:280:10)
2022-02-13T18:27:42.401382+00:00 app[web.1]: at /app/node_modules/mongoose/lib/connection.js:836:12
2022-02-13T18:27:42.401383+00:00 app[web.1]: at new Promise (<anonymous>)
2022-02-13T18:27:42.401383+00:00 app[web.1]: at NativeConnection.Connection.openUri (/app/node_modules/mongoose/lib/connection.js:832:19)
2022-02-13T18:27:42.401384+00:00 app[web.1]: at /app/node_modules/mongoose/lib/index.js:351:10
2022-02-13T18:27:42.401384+00:00 app[web.1]: at /app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
2022-02-13T18:27:42.401384+00:00 app[web.1]: at new Promise (<anonymous>)
2022-02-13T18:27:42.401384+00:00 app[web.1]: at promiseOrCallback (/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
2022-02-13T18:27:42.401384+00:00 app[web.1]: at Mongoose._promiseOrCallback (/app/node_modules/mongoose/lib/index.js:1149:10)
2022-02-13T18:27:42.401385+00:00 app[web.1]: at Mongoose.connect (/app/node_modules/mongoose/lib/index.js:350:20)
2022-02-13T18:27:42.401385+00:00 app[web.1]: at Object.<anonymous> (/app/server.js:19:10)
2022-02-13T18:27:42.401385+00:00 app[web.1]: at Module._compile (node:internal/modules/cjs/loader:1103:14)
2022-02-13T18:27:42.521294+00:00 heroku[web.1]: Process exited with status 1
2022-02-13T18:27:42.598710+00:00 heroku[web.1]: State changed from starting to crashed
2022-02-13T18:27:48.281004+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=budget-app-pwa4.herokuapp.com request_id=5de2651b-5f3f-4a1e-84f2-18c0123b8dd7 fwd="32.215.142.46" dyno= connect= service= status=503 bytes= protocol=https
2022-02-13T18:27:49.746492+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=budget-app-pwa4.herokuapp.com request_id=f15d0dc1-7a19-49e1-a743-13f31cac6c31 fwd="32.215.142.46" dyno= connect= service= status=503 bytes= protocol=https
This is my server.js file
const express = require("express");
const logger = require("morgan");
const mongoose = require("mongoose");
const compression = require("compression");
const PORT = process.env.PORT || 3001;
const MONGODB_URI = process.env.MONGODB_URI || "mongodb://localhost/budget";
const app = express();
app.use(logger("dev"));
app.use(compression());
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(express.static("public"));
mongoose.connect(MONGODB_URI, {
useNewUrlParser: true,
useFindAndModify: false,
useUnifiedTopology: true
});
// routes
app.use(require("./routes/api.js"));
app.listen(PORT, () => {
console.log(`App running on port ${PORT}!`);
});
Glancing at your log file, you can see that the top of the stack (at the point in which the exception is thrown) points to a parseConnectionString call. As you most likely deduced (since you included this in your title), the line above is a big clue as to what is going on:
MongoParseError: URI malformed, cannot be parsed
This indicates an issue with the connection string you are providing to the mongoose.connect function. Given the code you have shared, this can only be the result of an invalid process.env.MONGODB_URI value or the fallback mongodb://localhost/budget. Depending on how the MongoDB instance was provisioned, I might lean towards checking the value of the environment variable first, but if you are unsure about this, it might be best to first determine which of the two values your application is using (i.e., you could log this).
Overall, the issue appears to be related to the connection string you are using to connect to the MongoDB instance, and not explicitly caused by an issue with your application.
Hello and thank you everyone for helping. My issue was that I was using my Mongo username/password login info in the URI string, instead of my Mongo database username/password combination. I'm not sure if that makes any sense but I have it up and running. Thank you again!

Error while deploying app on Heroku MongoDB connection

I created the app using express and MongoDB. When I run the app locally it runs perfectly.I created a PostDB database on MongoDB atlas and generated and got connection string.I want to deploy this app using heroku but I don't why it gives this error.Please help me. Thanks in advance.
This is my connection string:
mongoose.connect("\mongodb+srv://Admin:minnu#mongodb01.irdlb.mongodb.net/PostDB",{useNewUrlParser:true,useCreateIndex:true, useUnifiedTopology: true},function(err,db){
if (err) {
console.log('Unable to connect to the server. Please start the server. Error:', err);
} else {
console.log('Connected to Server successfully!');
}});
This is the error I get:
2020-07-22T13:10:59.614060+00:00 app[web.1]: commonWireVersion: null
2020-07-22T13:10:59.614061+00:00 app[web.1]: }
2020-07-22T13:10:59.614061+00:00 app[web.1]: }
2020-07-22T13:10:59.631071+00:00 app[web.1]: Unable to connect to the server. Please start the server. Error: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
2020-07-22T13:10:59.631074+00:00 app[web.1]: at NativeConnection.Connection.openUri (/app/node_modules/mongoose/lib/connection.js:828:32)
2020-07-22T13:10:59.631075+00:00 app[web.1]: at Mongoose.connect (/app/node_modules/mongoose/lib/index.js:335:15)
2020-07-22T13:10:59.631076+00:00 app[web.1]: at Object.<anonymous> (/app/Models/User.js:2:10)
2020-07-22T13:10:59.631076+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1138:30)
2020-07-22T13:10:59.631077+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
2020-07-22T13:10:59.631077+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:986:32)
2020-07-22T13:10:59.631078+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:879:14)
2020-07-22T13:10:59.631078+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:1026:19)
2020-07-22T13:10:59.631079+00:00 app[web.1]: at require (internal/modules/cjs/helpers.js:72:18)
2020-07-22T13:10:59.631079+00:00 app[web.1]: at Object.<anonymous> (/app/Routes/Posts.js:6:12)
2020-07-22T13:10:59.631079+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1138:30)
2020-07-22T13:10:59.631080+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
2020-07-22T13:10:59.631080+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:986:32)
2020-07-22T13:10:59.631081+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:879:14)
2020-07-22T13:10:59.631081+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:1026:19)
2020-07-22T13:10:59.631081+00:00 app[web.1]: at require (internal/modules/cjs/helpers.js:72:18) {
2020-07-22T13:10:59.631082+00:00 app[web.1]: reason: TopologyDescription {
2020-07-22T13:10:59.631082+00:00 app[web.1]: type: 'Single',
2020-07-22T13:10:59.631083+00:00 app[web.1]: setName: null,
2020-07-22T13:10:59.631083+00:00 app[web.1]: maxSetVersion: null,
2020-07-22T13:10:59.631083+00:00 app[web.1]: maxElectionId: null,
2020-07-22T13:10:59.631084+00:00 app[web.1]: servers: Map { 'localhost:27017' => [ServerDescription] },
2020-07-22T13:10:59.631084+00:00 app[web.1]: stale: false,
2020-07-22T13:10:59.631085+00:00 app[web.1]: compatible: true,
2020-07-22T13:10:59.631085+00:00 app[web.1]: compatibilityError: null,
2020-07-22T13:10:59.631086+00:00 app[web.1]: logicalSessionTimeoutMinutes: null,
2020-07-22T13:10:59.631086+00:00 app[web.1]: heartbeatFrequencyMS: 10000,
2020-07-22T13:10:59.631086+00:00 app[web.1]: localThresholdMS: 15,
2020-07-22T13:10:59.631087+00:00 app[web.1]: commonWireVersion: null
2020-07-22T13:10:59.631087+00:00 app[web.1]: }
2020-07-22T13:10:59.631087+00:00 app[web.1]: }
2020-07-22T13:11:29.817201+00:00 heroku[web.1]: State changed from starting to up
2020-07-22T13:43:44.978055+00:00 heroku[web.1]: Idling
2020-07-22T13:43:44.980176+00:00 heroku[web.1]: State changed from up to down
2020-07-22T13:43:46.293776+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2020-07-22T13:43:46.385591+00:00 heroku[web.1]: Process exited with status 143
2020-07-22T15:23:20.000000+00:00 app[api]: Build started by user mamadgiaishwarya#gmail.com
2020-07-22T15:23:41.274124+00:00 app[api]: Deploy 9e2202b9 by user mamadgiaishwarya#gmail.com
2020-07-22T15:23:41.274124+00:00 app[api]: Release v9 created by user mamadgiaishwarya#gmail.com
2020-07-22T15:23:41.454375+00:00 heroku[web.1]: State changed from down to starting
2020-07-22T15:23:42.000000+00:00 app[api]: Build succeeded
2020-07-22T15:23:43.755247+00:00 heroku[web.1]: Starting process with command `npm start`
2020-07-22T15:23:46.020199+00:00 app[web.1]:
2020-07-22T15:23:46.020218+00:00 app[web.1]: > ejs-challenge#1.0.0 start /app
2020-07-22T15:23:46.020219+00:00 app[web.1]: > node app.js
2020-07-22T15:23:46.020219+00:00 app[web.1]:
2020-07-22T15:23:46.753513+00:00 app[web.1]: Warning: connect.session() MemoryStore is not
2020-07-22T15:23:46.753525+00:00 app[web.1]: designed for a production environment, as it will leak
2020-07-22T15:23:46.753554+00:00 app[web.1]: memory, and will not scale past a single process.
2020-07-22T15:23:46.770376+00:00 app[web.1]: Server started on port 3000
2020-07-22T15:23:47.305407+00:00 heroku[web.1]: State changed from starting to up
2020-07-22T15:23:48.056394+00:00 app[web.1]: Connected to Server successfully!
2020-07-22T15:23:48.059001+00:00 app[web.1]: Connected to Server successfully!
2020-07-22T15:24:25.538134+00:00 heroku[router]: at=info method=GET path="/" host=shielded-citadel-75286.herokuapp.com request_id=a2276768-c8b4-4937-a8f9-feb2e1cc9d00 fwd="106.200.146.62" dyno=web.1 connect=0ms service=1360ms status=200 bytes=4199 protocol=https
2020-07-22T15:24:26.201469+00:00 heroku[router]: at=info method=GET path="/css/styles.css" host=shielded-citadel-75286.herokuapp.com request_id=1d330f4c-66fd-407c-a8ed-57ba80507318 fwd="106.200.146.62" dyno=web.1 connect=1ms service=9ms status=200 bytes=2179 protocol=https
2020-07-22T15:24:26.284481+00:00 heroku[router]: at=info method=GET path="/images/logo.jpg" host=shielded-citadel-75286.herokuapp.com request_id=96a94f69-2984-4b7f-8e5e-1ae6f12493e6 fwd="106.200.146.62" dyno=web.1 connect=0ms service=11ms status=200 bytes=8897 protocol=https
2020-07-22T15:24:26.318935+00:00 heroku[router]: at=info method=GET path="/images/butterfly.gif" host=shielded-citadel-75286.herokuapp.com request_id=f54f11ef-31a4-4fdf-ad7f-c5d47c354afb fwd="106.200.146.62" dyno=web.1 connect=0ms service=45ms status=200 bytes=687572 protocol=https
2020-07-22T15:24:51.370552+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=shielded-citadel-75286.herokuapp.com request_id=396f3ab9-b44b-4432-888e-792eb45268e2 fwd="106.200.146.62" dyno=web.1 connect=0ms service=5ms status=404 bytes=394 protocol=https
2020-07-22T15:26:13.000000+00:00 app[api]: Build started by user mamadgiaishwarya#gmail.com
2020-07-22T15:26:35.089490+00:00 app[api]: Release v10 created by user mamadgiaishwarya#gmail.com
2020-07-22T15:26:35.089490+00:00 app[api]: Deploy 7fd231d2 by user mamadgiaishwarya#gmail.com
2020-07-22T15:26:36.000000+00:00 app[api]: Build succeeded
2020-07-22T15:26:36.726907+00:00 heroku[web.1]: Restarting
2020-07-22T15:26:36.741521+00:00 heroku[web.1]: State changed from up to starting
2020-07-22T15:26:37.654962+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2020-07-22T15:26:37.782161+00:00 heroku[web.1]: Process exited with status 143
2020-07-22T15:26:39.941929+00:00 heroku[web.1]: Starting process with command `npm start`
2020-07-22T15:26:42.451886+00:00 app[web.1]:
2020-07-22T15:26:42.451908+00:00 app[web.1]: > ejs-challenge#1.0.0 start /app
2020-07-22T15:26:42.451908+00:00 app[web.1]: > node app.js
2020-07-22T15:26:42.451909+00:00 app[web.1]:
2020-07-22T15:26:43.528332+00:00 app[web.1]: Warning: connect.session() MemoryStore is not
2020-07-22T15:26:43.528343+00:00 app[web.1]: designed for a production environment, as it will leak
2020-07-22T15:26:43.528344+00:00 app[web.1]: memory, and will not scale past a single process.
2020-07-22T15:26:43.537025+00:00 app[web.1]: Server started on port 3000
2020-07-22T15:26:43.977021+00:00 heroku[web.1]: State changed from starting to up
2020-07-22T15:26:44.829130+00:00 app[web.1]: Connected to Server successfully!
2020-07-22T15:26:44.831445+00:00 app[web.1]: Connected to Server successfully!
2020-07-22T15:28:23.000000+00:00 app[api]: Build started by user mamadgiaishwarya#gmail.com
2020-07-22T15:28:45.263212+00:00 app[api]: Deploy c7cbd9d0 by user mamadgiaishwarya#gmail.com
2020-07-22T15:28:45.263212+00:00 app[api]: Release v11 created by user mamadgiaishwarya#gmail.com
2020-07-22T15:28:45.446526+00:00 heroku[web.1]: Restarting
2020-07-22T15:28:45.448387+00:00 heroku[web.1]: State changed from up to starting
2020-07-22T15:28:46.000000+00:00 app[api]: Build succeeded
2020-07-22T15:28:46.651703+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2020-07-22T15:28:46.746865+00:00 heroku[web.1]: Process exited with status 143
2020-07-22T15:28:49.098450+00:00 heroku[web.1]: Starting process with command `npm start`
2020-07-22T15:28:51.924571+00:00 app[web.1]:
2020-07-22T15:28:51.924589+00:00 app[web.1]: > ejs-challenge#1.0.0 start /app
2020-07-22T15:28:51.924589+00:00 app[web.1]: > node app.js
2020-07-22T15:28:51.924589+00:00 app[web.1]:
2020-07-22T15:28:52.898117+00:00 app[web.1]: Warning: connect.session() MemoryStore is not
2020-07-22T15:28:52.898161+00:00 app[web.1]: designed for a production environment, as it will leak
2020-07-22T15:28:52.898162+00:00 app[web.1]: memory, and will not scale past a single process.
2020-07-22T15:28:52.905188+00:00 app[web.1]: Server started on port 3000
2020-07-22T15:28:53.428020+00:00 heroku[web.1]: State changed from starting to up
I think you have a mistake in your connection string.
According to MongoDB docs, connection string should start with mongodb://, not with the leading \ in the front.
I also recommend you to use Promises or Async/Await for your connections, like the following snippet below:
// Start connection via Mongoose.
mongoose
.connect(YOUR_CONNECTION_STRING, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true,
})
.then(() => {
console.log('Database connected successfully!');
})
.catch((err) => {
console.log('Error connecting with error code:', err);
});
app.listen(PORT, () => {
console.log('Server starts at port...');
}
It makes code much easier to read.
Alternatively, do you have any exceptions that you do not catch before connecting to the database? It might bubble up and crash your database connection for security reasons.

Error deploying node app to heroku

I'm relatively new to node, and have been having issues deploying a node web app to heroku... it seems to have to do with how I'm creating my mongoose connection in my server file. Here are the error logs I'm getting:
2016-01-06T00:41:30.384170+00:00 heroku[web.1]: State changed from starting to crashed
2016-01-06T00:43:56.191644+00:00 heroku[web.1]: State changed from crashed to starting
2016-01-06T00:43:57.774259+00:00 heroku[web.1]: Starting process with command `node server/server.js`
2016-01-06T00:44:00.138974+00:00 app[web.1]: listening on 4568
2016-01-06T00:44:00.172982+00:00 app[web.1]:
2016-01-06T00:44:00.172992+00:00 app[web.1]: /app/node_modules/mongoose/node_modules/mongodb/lib/server.js:236
2016-01-06T00:44:00.172994+00:00 app[web.1]: process.nextTick(function() { throw err; })
2016-01-06T00:44:00.172994+00:00 app[web.1]: ^
2016-01-06T00:44:00.172995+00:00 app[web.1]: Error: connect ECONNREFUSED 127.0.0.1:27017
2016-01-06T00:44:00.172996+00:00 app[web.1]: at Object.exports._errnoException (util.js:856:11)
2016-01-06T00:44:00.172997+00:00 app[web.1]: at exports._exceptionWithHostPort (util.js:879:20)
2016-01-06T00:44:00.172998+00:00 app[web.1]: at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1063:14)
2016-01-06T00:44:00.968046+00:00 heroku[web.1]: Process exited with status 1
2016-01-06T00:44:00.983259+00:00 heroku[web.1]: State changed from starting to crashed
And here is a shell of my server file:
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var entryController = require('./entries/entryController.js');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
app.use(express.static(__dirname + '/../client'));
mongoose.connect('mongodb://localhost');
console.log('listening on 4568');
app.listen(4568);
Any advice would be greatly appreciated, thanks!
You're trying to connect to localhost:
mongoose.connect('mongodb://localhost');
Localhost means "this computer." Your database is not running in the same container on Heroku as your node app, so instead you need to connect to where your database really is hosted.
You may want to read a tutorial like:
https://scotch.io/tutorials/use-mongodb-with-a-node-application-on-heroku

Resources