#node.js #heroku #heroku-api
#node.js #heroku #heroku-api
Вопрос:
Я пытаюсь развернуть очень простой API Nodejs на Heroku, но, похоже, я не могу понять, почему он продолжает выдавать мне эту ошибку. Приложение отлично работает на локальном сервере, даже с 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
Вот ошибка в консоли браузера:
Procfile:
web: node index.js
package.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" amp;amp; 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, но пока ничего не помогло: (
Ответ №1:
Используйте следующий код для установления соединения-
mongoose.connect(uri, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true
}).then(res=>{
console.log("DB Connected!")
}).catch(err => {
console.log(Error, err.message);
})
При использовании приведенного выше кода-
Вам необходимо внести в белый список IP-адрес heroku b, отправляемый в MongoDB Atlas.
Добавляемый IP-адрес — ‘0.0.0.0 / 0’
Комментарии:
1. Добавление IP-адреса было! Спасибо вам большое!