我正在使用Mern堆栈开发Web服务器。当我尝试使用猫鼬连接到我的mongodb Atlas集群时,出现错误。奇怪的是,当我尝试使用Compass连接到群集时,也会出现该错误。我将来自Atlas控制台的所有IP地址列入了白名单。我住在哥伦比亚,选择了GCP /圣保罗作为我的地区,那可能会成为问题的一部分吗?
这是我的服务器代码
const express = require('express');
const multer = require('multer');
const path = require('path');
let mongoose = require('mongoose');
const fileModel = require('./file_model.js');
let app = express();
let upload = multer();
mongoose.connect('mongodb+srv://username:password@psylockecluster-na7vy.gcp.mongodb.net/test', {useNewUrlParser: true, useUnifiedTopology:true}).catch(()=>console.log('did not connect'));
const db = mongoose.connection;
db.once('open', () => console.log('connected to mongo'));
db.on('error', (e) => console.log(e));
let port = process.env.PORT || 8000;
app.route('/interface').get((_, res) => {
try {
res.sendFile(__dirname + '/interface.html');
} catch (e) {
res.status(404).send(e.message);
}
})
app.post('/upload', upload.any(), (req, res) => {
let fileModels = req.files.map((file) => (
new fileModel({
root:path.dirname(file.fieldname),
fieldname:file.fieldname,
originalname:file.originalname,
encoding:file.encoding,
mimetype:file.mimetype,
buffer:file.buffer,
size:file.size
})
));
let test = fileModels[0];
test.save((err, test) => {
console.log('uploaded file');
if (err) {
console.log(err);
}
});
res.status(200).send("Files were uploaded succesfuly");
})
app.listen(port, () => console.log(`Server running on port ${port}`));
这是错误:
did not connect
Error: queryTxt ETIMEOUT cluster0-na7vy.mongodb.net
at QueryReqWrap.onresolve [as oncomplete] (dns.js:202:19) {
errno: 'ETIMEOUT',
code: 'ETIMEOUT',
syscall: 'queryTxt',
hostname: 'cluster0-na7vy.mongodb.net'
}