I m trying to deploy nodejs app on Heroku. This nodejs app has backend associated with MongoDB.At first, it looked like this problem is due to my local MongoDB database. And then I tried to connect MongoDB Atlas and got an error like invalid uri of MongoDB Atlas. Could someone please tell me how to deploy nodejs app with MongoDB on Heroku? Here are the "Heroku logs".
2019-11-05T06:03:03.996258+00:00 app[web.1]: Warning: connect.session() MemoryStore is not
2019-11-05T06:03:03.996302+00:00 app[web.1]: designed for a production environment, as it will leak
2019-11-05T06:03:03.996305+00:00 app[web.1]: memory, and will not scale past a single process.
2019-11-05T06:03:04.000947+00:00 app[web.1]: (node:23) DeprecationWarning: `open()` is deprecated in mongoose >= 4.11.0, use `openUri()` instead, or set the `useMongoClient` option if using `connect()` or `createConnection()`. See http://mongoosejs.com/docs/4.x/docs/connections.html#use-mongo-client
2019-11-05T06:03:04.008329+00:00 app[web.1]:
2019-11-05T06:03:04.008332+00:00 app[web.1]: events.js:187
2019-11-05T06:03:04.008335+00:00 app[web.1]: throw er; // Unhandled 'error' event
2019-11-05T06:03:04.008337+00:00 app[web.1]: ^
2019-11-05T06:03:04.009548+00:00 app[web.1]: Error [MongoError]: failed to connect to server [localhost:27017] on first connect [Error: connect ECONNREFUSED 127.0.0.1:27017
2019-11-05T06:03:04.009552+00:00 app[web.1]: at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1128:14) {
2019-11-05T06:03:04.009554+00:00 app[web.1]: name: 'MongoError',
2019-11-05T06:03:04.009556+00:00 app[web.1]: message: 'connect ECONNREFUSED 127.0.0.1:27017'
2019-11-05T06:03:04.009559+00:00 app[web.1]: }]
2019-11-05T06:03:04.009561+00:00 app[web.1]: at Pool.<anonymous> (/app/node_modules/mongodb-core/lib/topologies/server.js:336:35)
2019-11-05T06:03:04.009564+00:00 app[web.1]: at Pool.emit (events.js:210:5)
2019-11-05T06:03:04.009566+00:00 app[web.1]: at Connection.<anonymous> (/app/node_modules/mongodb-core/lib/connection/pool.js:280:12)
2019-11-05T06:03:04.009569+00:00 app[web.1]: at Object.onceWrapper (events.js:300:26)
2019-11-05T06:03:04.009571+00:00 app[web.1]: at Connection.emit (events.js:210:5)
2019-11-05T06:03:04.009573+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mongodb-core/lib/connection/connection.js:189:49)
2019-11-05T06:03:04.009575+00:00 app[web.1]: at Object.onceWrapper (events.js:300:26)
2019-11-05T06:03:04.009577+00:00 app[web.1]: at Socket.emit (events.js:210:5)
2019-11-05T06:03:04.009579+00:00 app[web.1]: at emitErrorNT (internal/streams/destroy.js:92:8)
2019-11-05T06:03:04.009581+00:00 app[web.1]: at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
2019-11-05T06:03:04.009608+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:80:21)
2019-11-05T06:03:04.00961+00:00 app[web.1]: Emitted 'error' event on NativeConnection instance at:
2019-11-05T06:03:04.009612+00:00 app[web.1]: at Immediate.<anonymous> (/app/node_modules/mongoose/lib/connection.js:325:19)
2019-11-05T06:03:04.009614+00:00 app[web.1]: at processImmediate (internal/timers.js:439:21) {
2019-11-05T06:03:04.009617+00:00 app[web.1]: name: 'MongoError',
2019-11-05T06:03:04.009619+00:00 app[web.1]: message: 'failed to connect to server [localhost:27017] on first connect [Error: connect ECONNREFUSED 127.0.0.1:27017\n' +
2019-11-05T06:03:04.009621+00:00 app[web.1]: ' at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1128:14) {\n' +
2019-11-05T06:03:04.009623+00:00 app[web.1]: " name: 'MongoError',\n" +
2019-11-05T06:03:04.009625+00:00 app[web.1]: " message: 'connect ECONNREFUSED 127.0.0.1:27017'\n" +
2019-11-05T06:03:04.009628+00:00 app[web.1]: '}]'
2019-11-05T06:03:04.00963+00:00 app[web.1]: }
2019-11-05T06:03:04.021847+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2019-11-05T06:03:04.022295+00:00 app[web.1]: npm ERR! errno 1
2019-11-05T06:03:04.023775+00:00 app[web.1]: npm ERR! [email protected] start: `node ./bin/www`
2019-11-05T06:03:04.023949+00:00 app[web.1]: npm ERR! Exit status 1
2019-11-05T06:03:04.024123+00:00 app[web.1]: npm ERR!
2019-11-05T06:03:04.024263+00:00 app[web.1]: npm ERR! Failed at the [email protected] start script.
2019-11-05T06:03:04.024373+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2019-11-05T06:03:04.033623+00:00 app[web.1]:
2019-11-05T06:03:04.033825+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2019-11-05T06:03:04.034004+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2019-11-05T06_03_04_027Z-debug.log
2019-11-05T06:03:04.105692+00:00 heroku[web.1]: Process exited with status 1
2019-11-05T06:03:04.15384+00:00 heroku[web.1]: State changed from starting to crashed
2019-11-05T06:03:04.156705+00:00 heroku[web.1]: State changed from crashed to starting
2019-11-05T06:03:06.6284+00:00 heroku[web.1]: Starting process with command `npm start`
2019-11-05T06:03:08.579954+00:00 app[web.1]:
2019-11-05T06:03:08.579982+00:00 app[web.1]: > [email protected] start /app
2019-11-05T06:03:08.579985+00:00 app[web.1]: > node ./bin/www
2019-11-05T06:03:08.579987+00:00 app[web.1]:
2019-11-05T06:03:09.157467+00:00 app[web.1]: Warning: connect.session() MemoryStore is not
2019-11-05T06:03:09.1575+00:00 app[web.1]: designed for a production environment, as it will leak
2019-11-05T06:03:09.157502+00:00 app[web.1]: memory, and will not scale past a single process.
2019-11-05T06:03:09.163289+00:00 app[web.1]: (node:23) DeprecationWarning: `open()` is deprecated in mongoose >= 4.11.0, use `openUri()` instead, or set the `useMongoClient` option if using `connect()` or `createConnection()`. See http://mongoosejs.com/docs/4.x/docs/connections.html#use-mongo-client
2019-11-05T06:03:09.170639+00:00 app[web.1]:
2019-11-05T06:03:09.170643+00:00 app[web.1]: events.js:187
2019-11-05T06:03:09.170645+00:00 app[web.1]: throw er; // Unhandled 'error' event
2019-11-05T06:03:09.170647+00:00 app[web.1]: ^
2019-11-05T06:03:09.171829+00:00 app[web.1]: Error [MongoError]: failed to connect to server [localhost:27017] on first connect [Error: connect ECONNREFUSED 127.0.0.1:27017
2019-11-05T06:03:09.171832+00:00 app[web.1]: at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1128:14) {
2019-11-05T06:03:09.171835+00:00 app[web.1]: name: 'MongoError',
2019-11-05T06:03:09.171837+00:00 app[web.1]: message: 'connect ECONNREFUSED 127.0.0.1:27017'
2019-11-05T06:03:09.171839+00:00 app[web.1]: }]
2019-11-05T06:03:09.171841+00:00 app[web.1]: at Pool.<anonymous> (/app/node_modules/mongodb-core/lib/topologies/server.js:336:35)
2019-11-05T06:03:09.171843+00:00 app[web.1]: at Pool.emit (events.js:210:5)
2019-11-05T06:03:09.171845+00:00 app[web.1]: at Connection.<anonymous> (/app/node_modules/mongodb-core/lib/connection/pool.js:280:12)
2019-11-05T06:03:09.171847+00:00 app[web.1]: at Object.onceWrapper (events.js:300:26)
2019-11-05T06:03:09.17185+00:00 app[web.1]: at Connection.emit (events.js:210:5)
2019-11-05T06:03:09.171851+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mongodb-core/lib/connection/connection.js:189:49)
2019-11-05T06:03:09.171853+00:00 app[web.1]: at Object.onceWrapper (events.js:300:26)
2019-11-05T06:03:09.171855+00:00 app[web.1]: at Socket.emit (events.js:210:5)
2019-11-05T06:03:09.171858+00:00 app[web.1]: at emitErrorNT (internal/streams/destroy.js:92:8)
2019-11-05T06:03:09.17186+00:00 app[web.1]: at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
2019-11-05T06:03:09.171862+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:80:21)
2019-11-05T06:03:09.171864+00:00 app[web.1]: Emitted 'error' event on NativeConnection instance at:
2019-11-05T06:03:09.171866+00:00 app[web.1]: at Immediate.<anonymous> (/app/node_modules/mongoose/lib/connection.js:325:19)
2019-11-05T06:03:09.171868+00:00 app[web.1]: at processImmediate (internal/timers.js:439:21) {
2019-11-05T06:03:09.17187+00:00 app[web.1]: name: 'MongoError',
2019-11-05T06:03:09.171872+00:00 app[web.1]: message: 'failed to connect to server [localhost:27017] on first connect [Error: connect ECONNREFUSED 127.0.0.1:27017\n' +
2019-11-05T06:03:09.171874+00:00 app[web.1]: ' at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1128:14) {\n' +
2019-11-05T06:03:09.171876+00:00 app[web.1]: " name: 'MongoError',\n" +
2019-11-05T06:03:09.171878+00:00 app[web.1]: " message: 'connect ECONNREFUSED 127.0.0.1:27017'\n" +
2019-11-05T06:03:09.17188+00:00 app[web.1]: '}]'
2019-11-05T06:03:09.171883+00:00 app[web.1]: }
2019-11-05T06:03:09.178432+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2019-11-05T06:03:09.178704+00:00 app[web.1]: npm ERR! errno 1
2019-11-05T06:03:09.179885+00:00 app[web.1]: npm ERR! [email protected] start: `node ./bin/www`
2019-11-05T06:03:09.18004+00:00 app[web.1]: npm ERR! Exit status 1
2019-11-05T06:03:09.180218+00:00 app[web.1]: npm ERR!
2019-11-05T06:03:09.180416+00:00 app[web.1]: npm ERR! Failed at the [email protected] start script.
2019-11-05T06:03:09.180597+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2019-11-05T06:03:09.190373+00:00 app[web.1]:
2019-11-05T06:03:09.190541+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2019-11-05T06:03:09.190691+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2019-11-05T06_03_09_182Z-debug.log
2019-11-05T06:03:09.239698+00:00 heroku[web.1]: Process exited with status 1
2019-11-05T06:03:09.281141+00:00 heroku[web.1]: State changed from starting to crashed
2019-11-05T06:03:22.201926+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=booquarium.herokuapp.com request_id=d7a35d48-d4c9-471e-b2c4-06dd21427bc2 fwd="27.34.106.90" dyno= connect= service= status=503 bytes= protocol=http
2019-11-05T06:03:23.305051+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=booquarium.herokuapp.com request_id=803ae941-6397-4eb7-8ee8-cd28dacdf728 fwd="27.34.106.90" dyno= connect= service= status=503 bytes= protocol=http
After seeing this i came to realize that it must be related with my local MongoDB database. Then I tried to connect MongoDB Atlas with
const { DB_URL } = config;
mongoose.connect(DB_URL,{
useUnifiedTopology: true,
useNewUrlParser: true
})
.then(()=>console.log('Connected to the database'))
.catch(err=> console.log('could not connect.',err.message))
And got an error like
> could not connect. Invalid mongodb uri
> "mongodb+srv://user:[email protected]/test?retryWrites=true&w=majority".
> Must begin with "mongodb://" (node:17676) DeprecationWarning: `open()`
> is deprecated in mongoose >= 4.11.0, use `openUri()` instead, or set
> the `useMongoClient` option if using `connect()` or
> `createConnection()`. See
> http://mongoosejs.com/docs/4.x/docs/connections.html#use-mongo-client
> (node:17676) DeprecationWarning: Mongoose: mpromise (mongoose's
> default promise library) is deprecated, plug in your own promise
> library instead: http://mongoosejs.com/docs/promises.html
I don't know what to do? Could someone please help me deploying this nodejs app on Heroku?


