简体   繁体   English

无法连接到mongolab数据库

[英]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.

相关问题 如何在没有 MongoLab 的情况下将本地 MongoDB 数据库连接到我的 Android Studio 应用程序? - How Do I connect a local MongoDB database to my Android Studio Application without MongoLab? 如何在AngularJS中连接并放入JSON而不是mongolab? - How to connect and put in JSON instead of mongolab in AngularJS? MongoError:身份验证失败。 -无法连接到MongoLab - MongoError: Authentication failed. - trouble connecting to MongoLab 使用Node.js在MongoLab中连接到mongoDB时出错 - Getting error while connect to mongoDB in MongoLab using Node.js MongoDB / MongoLab删除请求在本地有效,但实时删除数据库 - MongoDB / MongoLab Remove request works locally but deletes database when live MikroORM 无法连接到 postgresql://postgres@127.0.0.1:5432 上的数据库 lireddit - MikroORM failed to connect to database lireddit on postgresql://postgres@127.0.0.1:5432 如何使用node.js后端服务器与前端和mongolab数据库进行交互? - How to use a node.js back-end server to interact with the front-end and a mongolab database? 首次连接失败时如何在 Node.js 中重试数据库连接? - How to retry database connection in Node.js when first connect is failed? 无法连接到 localhost 5000 - Failed to connect to localhost 5000 无法连接到mongoose实验室 - failed to connect to mongoose lab
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM