带有 connect-mongo MongoServerSelectionError 的 Express 会话

问题描述 投票:0回答:1

我是 MERN 堆栈的新手。我正在从教程中学习有关 connect-mongo 的快速会话,但我不断收到以下错误:

**const timeoutError = new error_1.MongoServerSelectionError(Server selection timed out after ${options.serverSelectionTimeoutMS} ms, this.description);
                                 ^
MongoServerSelectionError: connect ECONNREFUSED ::1:27017
    at EventTarget.<anonymous> (D:\udemy2\authentication\passportJS\node_modules\mongodb\lib\sdam\topology.js:276:34)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:741:20)
    at EventTarget.dispatchEvent (node:internal/event_target:683:26)
    at abortSignal (node:internal/abort_controller:368:10)
    at TimeoutController.abort (node:internal/abort_controller:402:5)
    at Timeout.<anonymous> (D:\udemy2\authentication\passportJS\node_modules\mongodb\lib\utils.js:1010:92)
    at listOnTimeout (node:internal/timers:569:17)
    at process.processTimers (node:internal/timers:512:7) {
  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: 734535,
        lastWriteDate: 0,
        error: MongoNetworkError: connect ECONNREFUSED ::1:27017
            at connectionFailureError (D:\udemy2\authentication\passportJS\node_modules\mongodb\lib\cmap\connect.js:379:20)
            at Socket.<anonymous> (D:\udemy2\authentication\passportJS\node_modules\mongodb\lib\cmap\connect.js:285:22)
            at Object.onceWrapper (node:events:629:26)
            at Socket.emit (node:events:514:28)
            at emitErrorNT (node:internal/streams/destroy:151:8)
            at emitErrorCloseNT (node:internal/streams/destroy:116:3)
            at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
          [Symbol(errorLabels)]: Set(1) { 'ResetPool' },
          [cause]: Error: connect ECONNREFUSED ::1:27017
              at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1495:16) {
            errno: -4078,
            code: 'ECONNREFUSED',
            syscall: 'connect',
            address: '::1',
            port: 27017
          }
        },
        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,
  [Symbol(errorLabels)]: Set(0) {},
  [cause]: MongoNetworkError: connect ECONNREFUSED ::1:27017
      at connectionFailureError (D:\udemy2\authentication\passportJS\node_modules\mongodb\lib\cmap\connect.js:379:20)
      at Socket.<anonymous> (D:\udemy2\authentication\passportJS\node_modules\mongodb\lib\cmap\connect.js:285:22)
      at Object.onceWrapper (node:events:629:26)
      at Socket.emit (node:events:514:28)
      at emitErrorNT (node:internal/streams/destroy:151:8)
      at emitErrorCloseNT (node:internal/streams/destroy:116:3)
      at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
    [Symbol(errorLabels)]: Set(1) { 'ResetPool' },
    [cause]: Error: connect ECONNREFUSED ::1:27017
        at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1495:16) {
      errno: -4078,
      code: 'ECONNREFUSED',
      syscall: 'connect',
      address: '::1',
      port: 27017
    }
  }
}.**

我做了一些研究,它说这可能是因为我的服务器没有运行,但是当我打开“服务”时,它说它已启动。我尝试多次重新启动它,但没有帮助。 这是我的代码:

const express = require("express");
const session = require("express-session");
const mongoose = require("mongoose")
const MongoStore = require("connect-mongo");

const app = express();

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

const connection = mongoose.createConnection("mongodb://127.0.0.1/testDB", {useNewUrlParser: true})

app.use(session({
    secret: 'keyboard cat',
    saveUninitialized: false, // don't create session until something stored
    resave: false, //don't save session if unmodified
    store: MongoStore.create({
      mongoUrl: 'mongodb://localhost/test-app',
      touchAfter: 24 * 3600 // time period in seconds
    })
  }));

app.get("/", (req,res)=>{
    res.send("...")
})

app.listen(3000, ()=>{
    console.log("server is running")
})

提前致谢

express mongoose mern express-session connect-mongo
1个回答
0
投票

如果您确定 mongo 服务器正在运行,那么您的应用程序无法连接到 mongo 服务器,在这种情况下请检查您的 Mongo conf 文件。

Linux: /etc/mongod.conf
macOS: /usr/local/etc/mongod.conf (on Intel processors), or /opt/homebrew/etc/mongod.conf (on apple M1 processors)
Windows: <install directory>\bin\mongod.cfg

你的bindip值应该是这样的

net:
  bindIp: 127.0.0.1, ::1
  ipv6: true
© www.soinside.com 2019 - 2024. All rights reserved.