**免责声明:**类似的主题没有为我的问题提供有效的解决方案!
- 已重新启动MongoDB服务器(发生错误时它仍在运行)
- 在Windows上使用MongoDB服务器作为服务(手动启动)
- 通过MongoDB Shell CLI Package建立连接,方法是在命令提示符中按Enter键建立默认连接(
mongodb://127.0.0.1:27017/directConnection=true&serverSelectionTimeoutMS=2000
) - 调用npm安装和npm启动(我的依赖项如下所示)
- 检查MongoDB是否正在运行
- 已通过Windows资源监视器检查端口27017是否已被使用TCP的mongod.exe占用,并且未受防火墙限制
- 已检查我没有使用可能会干扰的VPN或代理连接。
- 然后我打开了我正在收听的
http://localhost:3000/
(app.listen(3000);
)
但是,我仍然收到以下错误:
const timeoutError = new error_1.MongoServerSelectionError(`Server selection timed out after ${serverSelectionTimeoutMS} ms`, this.description); ^
MongoServerSelectionError: connect ECONNREFUSED ::1:27017
at Timeout._onTimeout (\node_modules\mongodb\lib\sdam\topology.js:330:38)
at listOnTimeout (node:internal/timers:557:17)
at processTimers (node:internal/timers:500:7) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) {
'localhost:27017' => ServerDescription {
_hostAddress: HostAddress { isIPv6: false, host: 'localhost', port: 27017 },
address: 'localhost:27017',
type: 'Unknown',
hosts: [],
passives: [],
arbiters: [],
tags: {},
maxWireVersion: 0,
roundTripTime: -1,
lastUpdateTime: 536295834,
lastWriteDate: 0,
error: MongoNetworkError: connect ECONNREFUSED ::1:27017
at connectionFailureError (\node_modules\mongodb\lib\cmap\connect.js:293:20)
at Socket.<anonymous> (\node_modules\mongodb\lib\cmap\connect.js:267:22)
at Object.onceWrapper (node:events:510:26)
at Socket.emit (node:events:390:28)
at emitErrorNT (node:internal/streams/destroy:164:8)
at emitErrorCloseNT (node:internal/streams/destroy:129:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
}
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
logicalSessionTimeoutMinutes: undefined
}
}
我的相依性:
"dependencies": {
"ejs": "^3.1.6",
"express": "^4.17.1",
"mongodb": "^4.0.1"
},
"devDependencies": {
"nodemon": "^2.0.7"
}
仅供参考:节点. js v17.0.1
**更新:**这是我的database.js文件
const mongodb = require('mongodb');
const MongoClient = mongodb.MongoClient;
let database;
async function connectToDatabase() {
const client = await MongoClient.connect('mongodb://localhost:27017');
database = client.db('file-demo');
}
function getDb() {
if (!database) {
throw { message: 'Database not connected!' };
}
return database;
}
module.exports = {
connectToDatabase: connectToDatabase,
getDb: getDb,
};
这是我的app.js:
const path = require('path');
const express = require('express');
const userRoutes = require('./routes/users');
const db = require('./data/database');
const app = express();
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use(express.urlencoded({ extended: false }));
app.use(express.static('public'));
app.use(userRoutes);
db.connectToDatabase().then(function () {
app.listen(3000);
});
3条答案
按热度按时间unftdfkk1#
正如@prasad_所指出的,在建立MongoDB服务器连接的文件中,将
localhost
更改为127.0.0.1
会有所帮助。虽然它们应该被视为同义词,并且我能够排除一般问题(你可以在类似的问题中找到答案),但这可能只是chrome的缓存问题,就像here一样。e4yzc0pl2#
对我来说,问题是我安装了mongodb服务后没有运行它。
在mac上,运行:
mrzz3bfm3#
在我的示例中,我替换了hostname
本地主机
至
127.0.0.1
尝试更改
至