-1

我正在尝试在 Heroku 上部署非常简单的 Nodejs API,但我似乎无法弄清楚为什么它一直给我这个错误。该应用程序在本地服务器上运行良好,即使使用heroku local web. 我的应用正在监听app.listen( process.env.PORT || 5500 );.

这是我的错误 heroku logs --tail

2020-10-22T03:09:40.912659+00:00 app[web.1]: 'cluster0-shard-00-01.4mqda.mongodb.net:27017' => [ServerDescription]
2020-10-22T03:09:40.912659+00:00 app[web.1]: },
2020-10-22T03:09:40.912659+00:00 app[web.1]: stale: false,
2020-10-22T03:09:40.912660+00:00 app[web.1]: compatible: true,
2020-10-22T03:09:40.912660+00:00 app[web.1]: compatibilityError: null,
2020-10-22T03:09:40.912660+00:00 app[web.1]: logicalSessionTimeoutMinutes: null,
2020-10-22T03:09:40.912660+00:00 app[web.1]: heartbeatFrequencyMS: 10000,
2020-10-22T03:09:40.912661+00:00 app[web.1]: localThresholdMS: 15,
2020-10-22T03:09:40.912661+00:00 app[web.1]: commonWireVersion: null
2020-10-22T03:09:40.912661+00:00 app[web.1]: }
2020-10-22T03:09:40.912662+00:00 app[web.1]: }
2020-10-22T03:09:41.093698+00:00 heroku[web.1]: Process exited with status 0
2020-10-22T03:09:46.165964+00:00 heroku[web.1]: State changed from starting to crashed
2020-10-22T03:09:46.168346+00:00 heroku[web.1]: State changed from crashed to starting
2020-10-22T03:09:49.527931+00:00 heroku[web.1]: Starting process with command `node index.js`
2020-10-22T03:10:23.122929+00:00 app[web.1]: MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/
2020-10-22T03:10:23.123001+00:00 app[web.1]: at NativeConnection.Connection.openUri (/app/node_modules/mongoose/lib/connection.js:800:32)
2020-10-22T03:10:23.123006+00:00 app[web.1]: at /app/node_modules/mongoose/lib/index.js:342:10
2020-10-22T03:10:23.123010+00:00 app[web.1]: at /app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:5
2020-10-22T03:10:23.123015+00:00 app[web.1]: at new Promise (<anonymous>)
2020-10-22T03:10:23.123019+00:00 app[web.1]: at promiseOrCallback (/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:30:10)
2020-10-22T03:10:23.123020+00:00 app[web.1]: at Mongoose.connect (/app/node_modules/mongoose/lib/index.js:341:10)
2020-10-22T03:10:23.123020+00:00 app[web.1]: at Object.<anonymous> (/app/index.js:14:10)
2020-10-22T03:10:23.123020+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1200:30)
2020-10-22T03:10:23.123020+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
2020-10-22T03:10:23.123021+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:1049:32)
2020-10-22T03:10:23.123021+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:937:14)
2020-10-22T03:10:23.123021+00:00 app[web.1]: at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
2020-10-22T03:10:23.123022+00:00 app[web.1]: at internal/main/run_main_module.js:17:47 {
2020-10-22T03:10:23.123026+00:00 app[web.1]: reason: TopologyDescription {
2020-10-22T03:10:23.123026+00:00 app[web.1]: type: 'ReplicaSetNoPrimary',
2020-10-22T03:10:23.123026+00:00 app[web.1]: setName: null,
2020-10-22T03:10:23.123027+00:00 app[web.1]: maxSetVersion: null,
2020-10-22T03:10:23.123027+00:00 app[web.1]: maxElectionId: null,
2020-10-22T03:10:23.123027+00:00 app[web.1]: servers: Map(3) {
2020-10-22T03:10:23.123027+00:00 app[web.1]: 'cluster0-shard-00-02.4mqda.mongodb.net:27017' => [ServerDescription],
2020-10-22T03:10:23.123028+00:00 app[web.1]: 'cluster0-shard-00-00.4mqda.mongodb.net:27017' => [ServerDescription],
2020-10-22T03:10:23.123028+00:00 app[web.1]: 'cluster0-shard-00-01.4mqda.mongodb.net:27017' => [ServerDescription]
2020-10-22T03:10:23.123033+00:00 app[web.1]: },
2020-10-22T03:10:23.123033+00:00 app[web.1]: stale: false,
2020-10-22T03:10:23.123033+00:00 app[web.1]: compatible: true,
2020-10-22T03:10:23.123033+00:00 app[web.1]: compatibilityError: null,
2020-10-22T03:10:23.123034+00:00 app[web.1]: logicalSessionTimeoutMinutes: null,
2020-10-22T03:10:23.123034+00:00 app[web.1]: heartbeatFrequencyMS: 10000,
2020-10-22T03:10:23.123034+00:00 app[web.1]: localThresholdMS: 15,
2020-10-22T03:10:23.123034+00:00 app[web.1]: commonWireVersion: null
2020-10-22T03:10:23.123035+00:00 app[web.1]: }
2020-10-22T03:10:23.123035+00:00 app[web.1]: }
2020-10-22T03:10:23.187310+00:00 heroku[web.1]: Process exited with status 0
2020-10-22T03:10:23.226782+00:00 heroku[web.1]: State changed from starting to crashed
2020-10-22T03:10:24.274124+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=adak.herokuapp.com request_id=2e574211-23f3-423c-8322-2ec071a7f798 fwd="98.219.228.15" dyno= connect= service= status=503 bytes= protocol=https
2020-10-22T03:10:25.104524+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/app" host=adak.herokuapp.com request_id=53362476-b37c-487e-9453-0cce7aa0033d fwd="98.219.228.15" dyno= connect= service= status=503 bytes= protocol=https
2020-10-22T03:10:25.478442+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=adak.herokuapp.com request_id=a8c9df8f-4bfc-44a8-8061-ce6e0950f431 fwd="98.219.228.15" dyno= connect= service= status=503 bytes= protocol=https
2020-10-22T03:10:26.329468+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=adak.herokuapp.com request_id=8d56ea2d-257c-4118-9451-b454e59651f7 fwd="98.219.228.15" dyno= connect= service= status=503 bytes= protocol=https
2020-10-22T03:10:30.517993+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/app" host=adak.herokuapp.com request_id=8ae82052-e3d2-410c-90ed-e92e522acdeb fwd="98.219.228.15" dyno= connect= service= status=503 bytes= protocol=https
2020-10-22T03:10:30.857187+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=adak.herokuapp.com request_id=ff3b48e6-6712-4ab8-8937-07b0329fb97c fwd="98.219.228.15" dyno= connect= service= status=503 bytes= protocol=https
2020-10-22T03:13:50.120474+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=adak.herokuapp.com request_id=a94ad607-4349-44be-af8d-14d8cb31a497 fwd="98.219.228.15" dyno= connect= service= status=503 bytes= protocol=https
2020-10-22T03:13:50.386222+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=adak.herokuapp.com request_id=596442ac-c410-4c21-ab4b-fba6a358215f fwd="98.219.228.15" dyno= connect= service= status=503 bytes= protocol=https
2020-10-22T03:14:25.000000+00:00 app[api]: Build started by user kevinmnm2010@gmail.com
2020-10-22T03:14:46.000000+00:00 app[api]: Build succeeded
2020-10-22T03:14:46.007993+00:00 app[api]: Deploy e8565aab by user kevinmnm2010@gmail.com
2020-10-22T03:14:46.007993+00:00 app[api]: Release v4 created by user kevinmnm2010@gmail.com
2020-10-22T03:14:46.201472+00:00 heroku[web.1]: State changed from crashed to starting
2020-10-22T03:14:48.884957+00:00 heroku[web.1]: Starting process with command `node index.js`
2020-10-22T03:15:21.469714+00:00 app[web.1]: MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/
2020-10-22T03:15:21.477855+00:00 app[web.1]: at NativeConnection.Connection.openUri (/app/node_modules/mongoose/lib/connection.js:800:32)
2020-10-22T03:15:21.477862+00:00 app[web.1]: at /app/node_modules/mongoose/lib/index.js:342:10
2020-10-22T03:15:21.477867+00:00 app[web.1]: at /app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:5
2020-10-22T03:15:21.477871+00:00 app[web.1]: at new Promise (<anonymous>)
2020-10-22T03:15:21.477876+00:00 app[web.1]: at promiseOrCallback (/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:30:10)
2020-10-22T03:15:21.477876+00:00 app[web.1]: at Mongoose.connect (/app/node_modules/mongoose/lib/index.js:341:10)
2020-10-22T03:15:21.477876+00:00 app[web.1]: at Object.<anonymous> (/app/index.js:14:10)
2020-10-22T03:15:21.477877+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1200:30)
2020-10-22T03:15:21.477877+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
2020-10-22T03:15:21.477877+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:1049:32)
2020-10-22T03:15:21.477878+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:937:14)
2020-10-22T03:15:21.477878+00:00 app[web.1]: at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
2020-10-22T03:15:21.477878+00:00 app[web.1]: at internal/main/run_main_module.js:17:47 {
2020-10-22T03:15:21.477884+00:00 app[web.1]: reason: TopologyDescription {
2020-10-22T03:15:21.477884+00:00 app[web.1]: type: 'ReplicaSetNoPrimary',
2020-10-22T03:15:21.477884+00:00 app[web.1]: setName: null,
2020-10-22T03:15:21.477885+00:00 app[web.1]: maxSetVersion: null,
2020-10-22T03:15:21.477885+00:00 app[web.1]: maxElectionId: null,
2020-10-22T03:15:21.477885+00:00 app[web.1]: servers: Map(3) {
2020-10-22T03:15:21.477886+00:00 app[web.1]: 'cluster0-shard-00-02.4mqda.mongodb.net:27017' => [ServerDescription],
2020-10-22T03:15:21.477886+00:00 app[web.1]: 'cluster0-shard-00-00.4mqda.mongodb.net:27017' => [ServerDescription],
2020-10-22T03:15:21.477886+00:00 app[web.1]: 'cluster0-shard-00-01.4mqda.mongodb.net:27017' => [ServerDescription]
2020-10-22T03:15:21.477891+00:00 app[web.1]: },
2020-10-22T03:15:21.477877+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1200:30)
2020-10-22T03:15:21.477877+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
2020-10-22T03:15:21.477877+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:1049:32)
2020-10-22T03:15:21.477878+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:937:14)
2020-10-22T03:15:21.477878+00:00 app[web.1]: at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
2020-10-22T03:15:21.477878+00:00 app[web.1]: at internal/main/run_main_module.js:17:47 {
2020-10-22T03:15:21.477884+00:00 app[web.1]: reason: TopologyDescription {
2020-10-22T03:15:21.477884+00:00 app[web.1]: type: 'ReplicaSetNoPrimary',
2020-10-22T03:15:21.477884+00:00 app[web.1]: setName: null,
2020-10-22T03:15:21.477885+00:00 app[web.1]: maxSetVersion: null,
2020-10-22T03:15:21.477885+00:00 app[web.1]: maxElectionId: null,
2020-10-22T03:15:21.477885+00:00 app[web.1]: servers: Map(3) {
2020-10-22T03:15:21.477886+00:00 app[web.1]: 'cluster0-shard-00-02.4mqda.mongodb.net:27017' => [ServerDescription],
2020-10-22T03:15:21.477886+00:00 app[web.1]: 'cluster0-shard-00-00.4mqda.mongodb.net:27017' => [ServerDescription],
2020-10-22T03:15:21.477886+00:00 app[web.1]: 'cluster0-shard-00-01.4mqda.mongodb.net:27017' => [ServerDescription]
2020-10-22T03:15:21.477891+00:00 app[web.1]: },
2020-10-22T03:15:21.477892+00:00 app[web.1]: stale: false,
2020-10-22T03:15:21.477892+00:00 app[web.1]: compatible: true,
2020-10-22T03:15:21.477892+00:00 app[web.1]: compatibilityError: null,
2020-10-22T03:15:21.477893+00:00 app[web.1]: logicalSessionTimeoutMinutes: null,
2020-10-22T03:15:21.477893+00:00 app[web.1]: heartbeatFrequencyMS: 10000,
2020-10-22T03:15:21.477893+00:00 app[web.1]: localThresholdMS: 15,
2020-10-22T03:15:21.477893+00:00 app[web.1]: commonWireVersion: null
2020-10-22T03:15:21.477894+00:00 app[web.1]: }
2020-10-22T03:15:21.477894+00:00 app[web.1]: }
2020-10-22T03:15:21.561613+00:00 heroku[web.1]: Process exited with status 0
2020-10-22T03:15:21.656462+00:00 heroku[web.1]: State changed from starting to crashed
2020-10-22T03:15:23.967387+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=adak.herokuapp.com request_id=fe070797-e414-40c8-aeac-957b78fec27b fwd="98.219.228.15" dyno= connect= service= status=503 bytes= protocol=https

这是浏览器控制台上的错误: 在此处输入图像描述

档案:

web: node index.js

包.json

{
  "name": "adak-v3-server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "engines": {
    "node": "14.3.0"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node index.js"
  },
  "author": "",
  "license": "MIT",
  "dependencies": {
    "body-parser": "^1.19.0",
    "express": "^4.17.1",
    "mongodb": "^3.6.2",
    "mongoose": "^5.10.9",
    "serve": "^11.3.2"
  },
  "devDependencies": {
    "nodemon": "^2.0.6"
  }
}

我错过了什么?我在 Stackoverflow 上尝试了很多东西,但没有任何帮助:(

4

1 回答 1

1

使用以下代码进行连接 -

mongoose.connect(uri, {
      useNewUrlParser: true,
      useCreateIndex: true,
      useUnifiedTopology: true
    }).then(res=>{
            console.log("DB Connected!")
    }).catch(err => {
      console.log(Error, err.message);
    })

如果使用上面的代码
- 您需要将 Heroku IP 地址 b 列入 MongoDB Atlas 的白名单。
要添加的 IP 地址是 - '0.0.0.0/0'

于 2020-10-22T08:25:37.217 回答