[英]Failed to connect to mongolab database
Code Sample- 代码样本
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;
}
});
This is the code of my node.js. 这是我的node.js的代码。 I am using mongolab for mongodb.
我将mongolab用于mongodb。
I am running app on cloud9. 我正在cloud9上运行应用程序。
When i am connecting to the database using mongoose, it is throwing an error as failed to connect to 1%40ds029831.mongolab.com:29831 . 当我使用猫鼬连接到数据库时,由于无法连接到1%40ds029831.mongolab.com:29831而导致抛出错误。
App is connecting to the mongolab and after authentication (which is successful) it shows the this error. 应用正在连接到mongolab,并在身份验证(成功)之后显示此错误。 Please help me with this.
请帮我解决一下这个。
Does your password contains a '@' character? 您的密码是否包含“ @”字符? if yes please replace it with %40 and try again, the '@' character should only appear before the hostname.
如果是,请将其替换为%40,然后重试,“ @”字符应仅出现在主机名之前。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.