0

I know there are already alot of posts on this issue, but none of them worked or I didn't know how to do the fix they said. I am learning how to create a server witn node.js with this video: https://www.youtube.com/watch?v=1NrHkjlWVhM and now my server crash after 45 sec to 1 min.

This is the error code:

C:\Users\alexou\Documents\javascirpt\node.js server\node_modules\mongoose\lib\connection.js:824
  const serverSelectionError = new ServerSelectionError();
                               ^

MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
    at NativeConnection.Connection.openUri (C:\Users\alexou\Documents\javascirpt\node.js server\node_modules\mongoose\lib\connection.js:824:32)
    at C:\Users\alexou\Documents\javascirpt\node.js server\node_modules\mongoose\lib\index.js:417:10
    at C:\Users\alexou\Documents\javascirpt\node.js server\node_modules\mongoose\lib\helpers\promiseOrCallback.js:41:5
    at new Promise (<anonymous>)
    at promiseOrCallback (C:\Users\alexou\Documents\javascirpt\node.js server\node_modules\mongoose\lib\helpers\promiseOrCallback.js:40:10)
    at Mongoose._promiseOrCallback (C:\Users\alexou\Documents\javascirpt\node.js server\node_modules\mongoose\lib\index.js:1270:10)
    at Mongoose.connect (C:\Users\alexou\Documents\javascirpt\node.js server\node_modules\mongoose\lib\index.js:416:20)
    at Object.<anonymous> (C:\Users\alexou\Documents\javascirpt\node.js server\server.js:6:10)
    at Module._compile (node:internal/modules/cjs/loader:1126:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10) {
  reason: TopologyDescription {
    type: 'Unknown',
    servers: Map(1) {
      'localhost:27017' => ServerDescription {
        address: 'localhost:27017',
        type: 'Unknown',
        hosts: [],
        passives: [],
        arbiters: [],
        tags: {},
        minWireVersion: 0,
        maxWireVersion: 0,
        roundTripTime: -1,
        lastUpdateTime: 4373253,
        lastWriteDate: 0,
        error: MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017
            at connectionFailureError (C:\Users\alexou\Documents\javascirpt\node.js server\node_modules\mongodb\lib\cmap\connect.js:387:20)
            at Socket.<anonymous> (C:\Users\alexou\Documents\javascirpt\node.js server\node_modules\mongodb\lib\cmap\connect.js:310:22)
            at Object.onceWrapper (node:events:628:26)
            at Socket.emit (node:events:513:28)
            at emitErrorNT (node:internal/streams/destroy:157:8)
            at emitErrorCloseNT (node:internal/streams/destroy:122:3)
            at processTicksAndRejections (node:internal/process/task_queues:83:21) {
          cause: Error: connect ECONNREFUSED 127.0.0.1:27017
              at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1247:16) {
            errno: -4078,
            code: 'ECONNREFUSED',
            syscall: 'connect',
            address: '127.0.0.1',
            port: 27017
          },
          [Symbol(errorLabels)]: Set(1) { 'ResetPool' }
        },
        topologyVersion: null,
        setName: null,
        electionId: null,
        logicalSessionTimeoutMinutes: null,
        primary: null,
        me: null,
        '$clusterTime': null
      }
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: null,
    maxElectionId: null,
    maxSetVersion: null,
    commonWireVersion: 0,
    logicalSessionTimeoutMinutes: null
  },
  code: undefined
}
[nodemon] app crashed - waiting for file changes before starting...
[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
listening on port: 8080
C:\Users\alexou\Documents\javascirpt\node.js server\node_modules\mongoose\lib\connection.js:824
  const serverSelectionError = new ServerSelectionError();
                               ^

MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
    at NativeConnection.Connection.openUri (C:\Users\alexou\Documents\javascirpt\node.js server\node_modules\mongoose\lib\connection.js:824:32)
    at C:\Users\alexou\Documents\javascirpt\node.js server\node_modules\mongoose\lib\index.js:417:10
    at C:\Users\alexou\Documents\javascirpt\node.js server\node_modules\mongoose\lib\helpers\promiseOrCallback.js:41:5
    at new Promise (<anonymous>)
    at promiseOrCallback (C:\Users\alexou\Documents\javascirpt\node.js server\node_modules\mongoose\lib\helpers\promiseOrCallback.js:40:10)
    at Mongoose._promiseOrCallback (C:\Users\alexou\Documents\javascirpt\node.js server\node_modules\mongoose\lib\index.js:1270:10)
    at Mongoose.connect (C:\Users\alexou\Documents\javascirpt\node.js server\node_modules\mongoose\lib\index.js:416:20)
    at Object.<anonymous> (C:\Users\alexou\Documents\javascirpt\node.js server\server.js:6:10)
    at Module._compile (node:internal/modules/cjs/loader:1126:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10) {
  reason: TopologyDescription {
    type: 'Unknown',
    servers: Map(1) {
      'localhost:27017' => ServerDescription {
        address: 'localhost:27017',
        type: 'Unknown',
        hosts: [],
        passives: [],
        arbiters: [],
        tags: {},
        minWireVersion: 0,
        maxWireVersion: 0,
        roundTripTime: -1,
        lastUpdateTime: 18100265,
        lastWriteDate: 0,
        error: MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017
            at connectionFailureError (C:\Users\alexou\Documents\javascirpt\node.js server\node_modules\mongodb\lib\cmap\connect.js:387:20)
            at Socket.<anonymous> (C:\Users\alexou\Documents\javascirpt\node.js server\node_modules\mongodb\lib\cmap\connect.js:310:22)
            at Object.onceWrapper (node:events:628:26)
            at Socket.emit (node:events:513:28)
            at emitErrorNT (node:internal/streams/destroy:157:8)
            at emitErrorCloseNT (node:internal/streams/destroy:122:3)
            at processTicksAndRejections (node:internal/process/task_queues:83:21) {
          cause: Error: connect ECONNREFUSED 127.0.0.1:27017
              at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1247:16) {
            errno: -4078,
            code: 'ECONNREFUSED',
            syscall: 'connect',
            address: '127.0.0.1',
            port: 27017
          },
          [Symbol(errorLabels)]: Set(1) { 'ResetPool' }
        },
        topologyVersion: null,
        setName: null,
        setVersion: null,
        electionId: null,
        logicalSessionTimeoutMinutes: null,
        primary: null,
        me: null,
        '$clusterTime': null
      }
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: null,
    maxElectionId: null,
    maxSetVersion: null,
    commonWireVersion: 0,
    logicalSessionTimeoutMinutes: null
  },
  code: undefined
}
[nodemon] app crashed - waiting for file changes before starting...

And this is my code:

const express = require('express')
const mongoose = require('mongoose')
const articleRouter = require('./routes/articles')
const app = express()

mongoose.connect('mongodb://localhost/test', {useNewUrlParser: true, useUnifiedTopology: true})


app.set('view engine', 'ejs')


app.use(express.urlencoded({extended: false}))

app.get('/', (req, res) => {
    const articles = [{
        title: 'test',
        createdAt: new Date(),
        desctiption: 'test description'
    },
    {
        title: 'test 1',
        createdAt: new Date(),
        desctiption: 'test description 1'
    }]
    res.render('articles/index', { articles: articles })
})

app.listen(8080)
console.log(`listening on port: ${8080}`)

app.use('/articles', articleRouter)

How can I fix this error? Note: i also have other files in my project that I didn't include.

alexou
  • 37
  • 3
  • Do you have a database (`mongod` process) running on the same server as the application? That's what the code is trying to connect to, but `ECONNREFUSED` suggests that no such process is running – user20042973 Oct 28 '22 at 21:03
  • Often this error happens in my case if I don't have Internet. Try checking your internet – MegaMindTheCoder Oct 28 '22 at 21:36
  • 1
    "none of them worked" is pretty vague. Please specify what you tried and how it filed. As user2004 mentioned it's very generic error means nodejs process cannot establish tcp connection to the database. It doesn't explain the cause, and there are not enough information in the question to guess what might cause the issue and how to fix. – Alex Blex Oct 29 '22 at 00:03

0 Answers0