gpt4 book ai didi

javascript - 无法连接到 mongolab 数据库

转载 作者:行者123 更新时间:2023-11-28 19:26:54 27 4
gpt4 key购买 nike

代码示例-

const express=require('express'),
http=require('http'),
handlebars=require('express-handlebars').create({defaultLayout:'main'}),
fs=require('fs'),
mongoose=require('mongoose'),
uriUtil = require('mongodb-uri'),
credentials=require('./credentials.js'),
bodyParser=require('body-parser');

//connect to databse
var mongooptions = {
server: {
poolSize:5,
socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } },
replset: { socketOptions: { keepAlive: 1, connectTimeoutMS : 30000 }
},
};
var mongodbUri=("mongodb://user:pwd@ds029831.mongolab.com:29831/project");
var mongooseUri=uriUtil.formatMongoose(mongodbUri);
mongoose.connect(mongooseUri,mongooptions);
var conn=mongoose.connection;
conn.on('error',function(err){
console.log(err.message);
});

conn.on('connection',function() {
var app=express();
app.set('port',process.env.PORT);
app.engine('handlebars',handlebars.engine);
app.set('view engine','handlebars');
app.use(express.static('./public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(require('express-session')({
secret: 'intro skill',
resave: false,
saveUninitialized: false,
cookie: {maxAge:900000}
}));
var userSchema=new mongoose.Schema({
username: String,
password: String,
email : String
});
var user=mongoose.model('users',userSchema);
//routing

function startServer(){
http.createServer(app).listen(app.get('port'),function(){
console.log("Server Started at port "+app.get('port'));
});
}

if(require.main===module){
startServer();
}
else{
module.exports=startServer;
}

});

这是我的node.js 的代码。我正在使用 mongolab 作为 mongodb。
我正在 cloud9 上运行应用程序。
当我使用 mongoose 连接到数据库时,它抛出错误,无法连接到 1%40ds029831.mongolab.com:29831
应用程序正在连接到 mongolab,并且在身份验证(成功)后显示此错误。请帮我解决这个问题。

最佳答案

您的密码是否包含“@”字符?如果是,请将其替换为 %40,然后重试,“@”字符只能出现在主机名之前。

关于javascript - 无法连接到 mongolab 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27714431/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com