我正在尝试在Heroku上部署nodejs应用。这个nodejs应用程序具有与MongoDB关联的后端。起初,看来这个问题是由于我的本地MongoDB数据库引起的。然后,我尝试连接MongoDB Atlas,并收到错误消息,例如MongoDB Atlas的无效uri。有人可以告诉我如何在Heroku上使用MongoDB部署nodejs应用程序吗?这是“ Heroku日志”。
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! web@0.0.0 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 web@0.0.0 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]: > web@0.0.0 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! web@0.0.0 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 web@0.0.0 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
看到这一点后,我意识到它必须与我的本地MongoDB数据库相关。然后我尝试将MongoDB Atlas与
连接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))
并收到
之类的错误 > could not connect. Invalid mongodb uri
> "mongodb+srv://user:password@firstdatabase-ytrfr.mongodb.net/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
我不知道该怎么办?有人可以帮我在Heroku上部署这个nodejs应用程序吗?